3  * Alchemy Semi Au1000 pcmcia driver
 
   5  * Copyright 2001-2003 MontaVista Software Inc.
 
   6  * Author: MontaVista Software, Inc.
 
   7  *              ppopov@embeddedalley.com or source@mvista.com
 
   9  * Copyright 2004 Pete Popov, Embedded Alley Solutions, Inc.
 
  10  * Updated the driver to 2.6. Followed the sa11xx API and largely
 
  11  * copied many of the hardware independent functions.
 
  13  * ########################################################################
 
  15  *  This program is free software; you can distribute it and/or modify it
 
  16  *  under the terms of the GNU General Public License (Version 2) as
 
  17  *  published by the Free Software Foundation.
 
  19  *  This program is distributed in the hope it will be useful, but WITHOUT
 
  20  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
  21  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
  24  *  You should have received a copy of the GNU General Public License along
 
  25  *  with this program; if not, write to the Free Software Foundation, Inc.,
 
  26  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 
  28  * ########################################################################
 
  33 #include <linux/module.h>
 
  34 #include <linux/moduleparam.h>
 
  35 #include <linux/init.h>
 
  36 #include <linux/cpufreq.h>
 
  37 #include <linux/ioport.h>
 
  38 #include <linux/kernel.h>
 
  39 #include <linux/timer.h>
 
  41 #include <linux/notifier.h>
 
  42 #include <linux/interrupt.h>
 
  43 #include <linux/spinlock.h>
 
  44 #include <linux/platform_device.h>
 
  48 #include <asm/system.h>
 
  50 #include <asm/mach-au1x00/au1000.h>
 
  51 #include "au1000_generic.h"
 
  53 MODULE_LICENSE("GPL");
 
  54 MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
 
  55 MODULE_DESCRIPTION("Linux PCMCIA Card Services: Au1x00 Socket Controller");
 
  58 #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
 
  60 #define debug(x,args...)
 
  63 #define MAP_SIZE 0x100000
 
  64 extern struct au1000_pcmcia_socket au1000_pcmcia_socket[];
 
  65 #define PCMCIA_SOCKET(x)        (au1000_pcmcia_socket + (x))
 
  66 #define to_au1000_socket(x)     container_of(x, struct au1000_pcmcia_socket, socket)
 
  68 /* Some boards like to support CF cards as IDE root devices, so they
 
  69  * grab pcmcia sockets directly.
 
  71 u32 *pcmcia_base_vaddrs[2];
 
  72 extern const unsigned long mips_io_port_base;
 
  74 DECLARE_MUTEX(pcmcia_sockets_lock);
 
  76 static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
 
  81 au1x00_pcmcia_skt_state(struct au1000_pcmcia_socket *skt)
 
  83         struct pcmcia_state state;
 
  86         memset(&state, 0, sizeof(struct pcmcia_state));
 
  88         skt->ops->socket_state(skt, &state);
 
  90         stat = state.detect  ? SS_DETECT : 0;
 
  91         stat |= state.ready  ? SS_READY  : 0;
 
  92         stat |= state.wrprot ? SS_WRPROT : 0;
 
  93         stat |= state.vs_3v  ? SS_3VCARD : 0;
 
  94         stat |= state.vs_Xv  ? SS_XVCARD : 0;
 
  95         stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
 
  97         if (skt->cs_state.flags & SS_IOCARD)
 
  98                 stat |= state.bvd1 ? SS_STSCHG : 0;
 
 102                 else if (state.bvd2 == 0)
 
 109  * au100_pcmcia_config_skt
 
 111  * Convert PCMCIA socket state to our socket configure structure.
 
 114 au1x00_pcmcia_config_skt(struct au1000_pcmcia_socket *skt, socket_state_t *state)
 
 118         ret = skt->ops->configure_socket(skt, state);
 
 120                 skt->cs_state = *state;
 
 124                 debug("unable to configure socket %d\n", skt->nr);
 
 129 /* au1x00_pcmcia_sock_init()
 
 131  * (Re-)Initialise the socket, turning on status interrupts
 
 132  * and PCMCIA bus.  This must wait for power to stabilise
 
 133  * so that the card status signals report correctly.
 
 137 static int au1x00_pcmcia_sock_init(struct pcmcia_socket *sock)
 
 139         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
 
 141         debug("initializing socket %u\n", skt->nr);
 
 143         skt->ops->socket_init(skt);
 
 148  * au1x00_pcmcia_suspend()
 
 150  * Remove power on the socket, disable IRQs from the card.
 
 151  * Turn off status interrupts, and disable the PCMCIA bus.
 
 155 static int au1x00_pcmcia_suspend(struct pcmcia_socket *sock)
 
 157         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
 
 159         debug("suspending socket %u\n", skt->nr);
 
 161         skt->ops->socket_suspend(skt);
 
 166 static DEFINE_SPINLOCK(status_lock);
 
 169  * au1x00_check_status()
 
 171 static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
 
 175         debug("entering PCMCIA monitoring thread\n");
 
 181                 status = au1x00_pcmcia_skt_state(skt);
 
 183                 spin_lock_irqsave(&status_lock, flags);
 
 184                 events = (status ^ skt->status) & skt->cs_state.csc_mask;
 
 185                 skt->status = status;
 
 186                 spin_unlock_irqrestore(&status_lock, flags);
 
 188                 debug("events: %s%s%s%s%s%s\n",
 
 189                         events == 0         ? "<NONE>"   : "",
 
 190                         events & SS_DETECT  ? "DETECT "  : "",
 
 191                         events & SS_READY   ? "READY "   : "",
 
 192                         events & SS_BATDEAD ? "BATDEAD " : "",
 
 193                         events & SS_BATWARN ? "BATWARN " : "",
 
 194                         events & SS_STSCHG  ? "STSCHG "  : "");
 
 197                         pcmcia_parse_events(&skt->socket, events);
 
 202  * au1x00_pcmcia_poll_event()
 
 203  * Let's poll for events in addition to IRQs since IRQ only is unreliable...
 
 205 static void au1x00_pcmcia_poll_event(unsigned long dummy)
 
 207         struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
 
 208         debug("polling for events\n");
 
 210         mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
 
 212         au1x00_check_status(skt);
 
 215 /* au1x00_pcmcia_get_status()
 
 217  * From the sa11xx_core.c:
 
 218  * Implements the get_status() operation for the in-kernel PCMCIA
 
 219  * service (formerly SS_GetStatus in Card Services). Essentially just
 
 220  * fills in bits in `status' according to internal driver state or
 
 221  * the value of the voltage detect chipselect register.
 
 223  * As a debugging note, during card startup, the PCMCIA core issues
 
 224  * three set_socket() commands in a row the first with RESET deasserted,
 
 225  * the second with RESET asserted, and the last with RESET deasserted
 
 226  * again. Following the third set_socket(), a get_status() command will
 
 227  * be issued. The kernel is looking for the SS_READY flag (see
 
 228  * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
 
 233 au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
 
 235         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
 
 237         skt->status = au1x00_pcmcia_skt_state(skt);
 
 238         *status = skt->status;
 
 243 /* au1x00_pcmcia_set_socket()
 
 244  * Implements the set_socket() operation for the in-kernel PCMCIA
 
 245  * service (formerly SS_SetSocket in Card Services). We more or
 
 246  * less punt all of this work and let the kernel handle the details
 
 247  * of power configuration, reset, &c. We also record the value of
 
 248  * `state' in order to regurgitate it to the PCMCIA core later.
 
 253 au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
 
 255   struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
 
 257   debug("for sock %u\n", skt->nr);
 
 259   debug("\tmask:  %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
 
 260         (state->csc_mask==0)?"<NONE>":"",
 
 261         (state->csc_mask&SS_DETECT)?"DETECT ":"",
 
 262         (state->csc_mask&SS_READY)?"READY ":"",
 
 263         (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
 
 264         (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
 
 265         (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
 
 266         (state->flags==0)?"<NONE>":"",
 
 267         (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
 
 268         (state->flags&SS_IOCARD)?"IOCARD ":"",
 
 269         (state->flags&SS_RESET)?"RESET ":"",
 
 270         (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
 
 271         (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
 
 272   debug("\tVcc %d  Vpp %d  irq %d\n",
 
 273         state->Vcc, state->Vpp, state->io_irq);
 
 275   return au1x00_pcmcia_config_skt(skt, state);
 
 279 au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
 
 281         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
 
 284         if(map->map>=MAX_IO_WIN){
 
 285                 debug("map (%d) out of range\n", map->map);
 
 289         if(map->flags&MAP_ACTIVE){
 
 290                 speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
 
 291                 skt->spd_io[map->map] = speed;
 
 294         map->start=(ioaddr_t)(u32)skt->virt_io;
 
 295         map->stop=map->start+MAP_SIZE;
 
 298 }  /* au1x00_pcmcia_set_io_map() */
 
 302 au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
 
 304         struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
 
 305         unsigned short speed = map->speed;
 
 307         if(map->map>=MAX_WIN){
 
 308                 debug("map (%d) out of range\n", map->map);
 
 312         if (map->flags & MAP_ATTRIB) {
 
 313                 skt->spd_attr[map->map] = speed;
 
 314                 skt->spd_mem[map->map] = 0;
 
 316                 skt->spd_attr[map->map] = 0;
 
 317                 skt->spd_mem[map->map] = speed;
 
 320         if (map->flags & MAP_ATTRIB) {
 
 321                 map->static_start = skt->phys_attr + map->card_start;
 
 324                 map->static_start = skt->phys_mem + map->card_start;
 
 327         debug("set_mem_map %d start %08lx card_start %08x\n",
 
 328                         map->map, map->static_start, map->card_start);
 
 331 }  /* au1x00_pcmcia_set_mem_map() */
 
 333 static struct pccard_operations au1x00_pcmcia_operations = {
 
 334         .init                   = au1x00_pcmcia_sock_init,
 
 335         .suspend                = au1x00_pcmcia_suspend,
 
 336         .get_status             = au1x00_pcmcia_get_status,
 
 337         .set_socket             = au1x00_pcmcia_set_socket,
 
 338         .set_io_map             = au1x00_pcmcia_set_io_map,
 
 339         .set_mem_map            = au1x00_pcmcia_set_mem_map,
 
 342 static const char *skt_names[] = {
 
 347 struct skt_dev_info {
 
 351 int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
 
 353         struct skt_dev_info *sinfo;
 
 354         struct au1000_pcmcia_socket *skt;
 
 357         sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
 
 366          * Initialise the per-socket structure.
 
 368         for (i = 0; i < nr; i++) {
 
 369                 skt = PCMCIA_SOCKET(i);
 
 370                 memset(skt, 0, sizeof(*skt));
 
 372                 skt->socket.resource_ops = &pccard_static_ops;
 
 373                 skt->socket.ops = &au1x00_pcmcia_operations;
 
 374                 skt->socket.owner = ops->owner;
 
 375                 skt->socket.dev.parent = dev;
 
 377                 init_timer(&skt->poll_timer);
 
 378                 skt->poll_timer.function = au1x00_pcmcia_poll_event;
 
 379                 skt->poll_timer.data = (unsigned long)skt;
 
 380                 skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
 
 387                 skt->res_skt.name       = skt_names[skt->nr];
 
 388                 skt->res_io.name        = "io";
 
 389                 skt->res_io.flags       = IORESOURCE_MEM | IORESOURCE_BUSY;
 
 390                 skt->res_mem.name       = "memory";
 
 391                 skt->res_mem.flags      = IORESOURCE_MEM;
 
 392                 skt->res_attr.name      = "attribute";
 
 393                 skt->res_attr.flags     = IORESOURCE_MEM;
 
 396                  * PCMCIA client drivers use the inb/outb macros to access the
 
 397                  * IO registers. Since mips_io_port_base is added to the
 
 398                  * access address of the mips implementation of inb/outb,
 
 399                  * we need to subtract it here because we want to access the
 
 400                  * I/O or MEM address directly, without going through this
 
 401                  * "mips_io_port_base" mechanism.
 
 404                         skt->virt_io = (void *)
 
 405                                 (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
 
 406                                 (u32)mips_io_port_base);
 
 407                         skt->phys_attr = AU1X_SOCK0_PSEUDO_PHYS_ATTR;
 
 408                         skt->phys_mem = AU1X_SOCK0_PSEUDO_PHYS_MEM;
 
 410 #ifndef CONFIG_MIPS_XXS1500
 
 412                         skt->virt_io = (void *)
 
 413                                 (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
 
 414                                 (u32)mips_io_port_base);
 
 415                         skt->phys_attr = AU1X_SOCK1_PSEUDO_PHYS_ATTR;
 
 416                         skt->phys_mem = AU1X_SOCK1_PSEUDO_PHYS_MEM;
 
 419                 pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
 
 420                 ret = ops->hw_init(skt);
 
 422                 skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
 
 423                 skt->socket.irq_mask = 0;
 
 424                 skt->socket.map_size = MAP_SIZE;
 
 425                 skt->socket.pci_irq = skt->irq;
 
 426                 skt->socket.io_offset = (unsigned long)skt->virt_io;
 
 428                 skt->status = au1x00_pcmcia_skt_state(skt);
 
 430                 ret = pcmcia_register_socket(&skt->socket);
 
 434                 WARN_ON(skt->socket.sock != i);
 
 436                 add_timer(&skt->poll_timer);
 
 439         dev_set_drvdata(dev, sinfo);
 
 444         flush_scheduled_work();
 
 445         ops->hw_shutdown(skt);
 
 447                 skt = PCMCIA_SOCKET(i);
 
 449                 del_timer_sync(&skt->poll_timer);
 
 450                 pcmcia_unregister_socket(&skt->socket);
 
 451                 flush_scheduled_work();
 
 453                         iounmap(skt->virt_io + (u32)mips_io_port_base);
 
 456 #ifndef CONFIG_MIPS_XXS1500
 
 458                         iounmap(skt->virt_io + (u32)mips_io_port_base);
 
 462                 ops->hw_shutdown(skt);
 
 470 int au1x00_drv_pcmcia_remove(struct device *dev)
 
 472         struct skt_dev_info *sinfo = dev_get_drvdata(dev);
 
 475         down(&pcmcia_sockets_lock);
 
 476         dev_set_drvdata(dev, NULL);
 
 478         for (i = 0; i < sinfo->nskt; i++) {
 
 479                 struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
 
 481                 del_timer_sync(&skt->poll_timer);
 
 482                 pcmcia_unregister_socket(&skt->socket);
 
 483                 flush_scheduled_work();
 
 484                 skt->ops->hw_shutdown(skt);
 
 485                 au1x00_pcmcia_config_skt(skt, &dead_socket);
 
 486                 iounmap(skt->virt_io + (u32)mips_io_port_base);
 
 491         up(&pcmcia_sockets_lock);
 
 497  * PCMCIA "Driver" API
 
 500 static int au1x00_drv_pcmcia_probe(struct device *dev)
 
 502         int i, ret = -ENODEV;
 
 504         down(&pcmcia_sockets_lock);
 
 505         for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
 
 506                 ret = au1x00_pcmcia_hw_init[i](dev);
 
 510         up(&pcmcia_sockets_lock);
 
 515 static struct device_driver au1x00_pcmcia_driver = {
 
 516         .probe          = au1x00_drv_pcmcia_probe,
 
 517         .remove         = au1x00_drv_pcmcia_remove,
 
 518         .name           = "au1x00-pcmcia",
 
 519         .bus            = &platform_bus_type,
 
 520         .suspend        = pcmcia_socket_dev_suspend,
 
 521         .resume         = pcmcia_socket_dev_resume,
 
 525 /* au1x00_pcmcia_init()
 
 527  * This routine performs low-level PCMCIA initialization and then
 
 528  * registers this socket driver with Card Services.
 
 530  * Returns: 0 on success, -ve error code on failure
 
 532 static int __init au1x00_pcmcia_init(void)
 
 535         if ((error = driver_register(&au1x00_pcmcia_driver)))
 
 540 /* au1x00_pcmcia_exit()
 
 541  * Invokes the low-level kernel service to free IRQs associated with this
 
 542  * socket controller and reset GPIO edge detection.
 
 544 static void __exit au1x00_pcmcia_exit(void)
 
 546         driver_unregister(&au1x00_pcmcia_driver);
 
 549 module_init(au1x00_pcmcia_init);
 
 550 module_exit(au1x00_pcmcia_exit);