2  * Sonics Silicon Backplane
 
   3  * PCMCIA-Hostbus related functions
 
   5  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
 
   6  * Copyright 2007 Michael Buesch <mb@bu3sch.de>
 
   8  * Licensed under the GNU/GPL. See COPYING for details.
 
  11 #include <linux/ssb/ssb.h>
 
  12 #include <linux/delay.h>
 
  14 #include <pcmcia/cs_types.h>
 
  15 #include <pcmcia/cs.h>
 
  16 #include <pcmcia/cistpl.h>
 
  17 #include <pcmcia/ciscode.h>
 
  18 #include <pcmcia/ds.h>
 
  19 #include <pcmcia/cisreg.h>
 
  21 #include "ssb_private.h"
 
  24 /* Define the following to 1 to enable a printk on each coreswitch. */
 
  25 #define SSB_VERBOSE_PCMCIACORESWITCH_DEBUG              0
 
  28 int ssb_pcmcia_switch_coreidx(struct ssb_bus *bus,
 
  31         struct pcmcia_device *pdev = bus->host_pcmcia;
 
  39         addr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
 
  41                 reg.Action = CS_WRITE;
 
  43                 reg.Value = (addr & 0x0000F000) >> 12;
 
  44                 err = pcmcia_access_configuration_register(pdev, ®);
 
  45                 if (err != CS_SUCCESS)
 
  48                 reg.Value = (addr & 0x00FF0000) >> 16;
 
  49                 err = pcmcia_access_configuration_register(pdev, ®);
 
  50                 if (err != CS_SUCCESS)
 
  53                 reg.Value = (addr & 0xFF000000) >> 24;
 
  54                 err = pcmcia_access_configuration_register(pdev, ®);
 
  55                 if (err != CS_SUCCESS)
 
  62                 err = pcmcia_access_configuration_register(pdev, ®);
 
  63                 if (err != CS_SUCCESS)
 
  65                 read_addr |= (reg.Value & 0xF) << 12;
 
  67                 err = pcmcia_access_configuration_register(pdev, ®);
 
  68                 if (err != CS_SUCCESS)
 
  70                 read_addr |= reg.Value << 16;
 
  72                 err = pcmcia_access_configuration_register(pdev, ®);
 
  73                 if (err != CS_SUCCESS)
 
  75                 read_addr |= reg.Value << 24;
 
  77                 cur_core = (read_addr - SSB_ENUM_BASE) / SSB_CORE_SIZE;
 
  78                 if (cur_core == coreidx)
 
  81                 if (attempts++ > SSB_BAR0_MAX_RETRIES)
 
  88         ssb_printk(KERN_ERR PFX "Failed to switch to core %u\n", coreidx);
 
  92 int ssb_pcmcia_switch_core(struct ssb_bus *bus,
 
  93                            struct ssb_device *dev)
 
  98 #if SSB_VERBOSE_PCMCIACORESWITCH_DEBUG
 
  99         ssb_printk(KERN_INFO PFX
 
 100                    "Switching to %s core, index %d\n",
 
 101                    ssb_core_name(dev->id.coreid),
 
 105         spin_lock_irqsave(&bus->bar_lock, flags);
 
 106         err = ssb_pcmcia_switch_coreidx(bus, dev->core_index);
 
 108                 bus->mapped_device = dev;
 
 109         spin_unlock_irqrestore(&bus->bar_lock, flags);
 
 114 int ssb_pcmcia_switch_segment(struct ssb_bus *bus, u8 seg)
 
 121         SSB_WARN_ON((seg != 0) && (seg != 1));
 
 124         spin_lock_irqsave(&bus->bar_lock, flags);
 
 126                 reg.Action = CS_WRITE;
 
 128                 res = pcmcia_access_configuration_register(bus->host_pcmcia, ®);
 
 129                 if (unlikely(res != CS_SUCCESS))
 
 132                 reg.Action = CS_READ;
 
 133                 res = pcmcia_access_configuration_register(bus->host_pcmcia, ®);
 
 134                 if (unlikely(res != CS_SUCCESS))
 
 137                 if (reg.Value == seg)
 
 140                 if (unlikely(attempts++ > SSB_BAR0_MAX_RETRIES))
 
 144         bus->mapped_pcmcia_seg = seg;
 
 146         spin_unlock_irqrestore(&bus->bar_lock, flags);
 
 149         ssb_printk(KERN_ERR PFX "Failed to switch pcmcia segment\n");
 
 154 /* These are the main device register access functions.
 
 155  * do_select_core is inline to have the likely hotpath inline.
 
 156  * All unlikely codepaths are out-of-line. */
 
 157 static inline int do_select_core(struct ssb_bus *bus,
 
 158                                  struct ssb_device *dev,
 
 162         u8 need_seg = (*offset >= 0x800) ? 1 : 0;
 
 164         if (unlikely(dev != bus->mapped_device)) {
 
 165                 err = ssb_pcmcia_switch_core(bus, dev);
 
 169         if (unlikely(need_seg != bus->mapped_pcmcia_seg)) {
 
 170                 err = ssb_pcmcia_switch_segment(bus, need_seg);
 
 180 static u16 ssb_pcmcia_read16(struct ssb_device *dev, u16 offset)
 
 182         struct ssb_bus *bus = dev->bus;
 
 185         if (unlikely(do_select_core(bus, dev, &offset)))
 
 187         x = readw(bus->mmio + offset);
 
 192 static u32 ssb_pcmcia_read32(struct ssb_device *dev, u16 offset)
 
 194         struct ssb_bus *bus = dev->bus;
 
 197         if (unlikely(do_select_core(bus, dev, &offset)))
 
 199         x = readl(bus->mmio + offset);
 
 204 static void ssb_pcmcia_write16(struct ssb_device *dev, u16 offset, u16 value)
 
 206         struct ssb_bus *bus = dev->bus;
 
 208         if (unlikely(do_select_core(bus, dev, &offset)))
 
 210         writew(value, bus->mmio + offset);
 
 213 static void ssb_pcmcia_write32(struct ssb_device *dev, u16 offset, u32 value)
 
 215         struct ssb_bus *bus = dev->bus;
 
 217         if (unlikely(do_select_core(bus, dev, &offset)))
 
 219         readw(bus->mmio + offset);
 
 220         writew(value >> 16, bus->mmio + offset + 2);
 
 221         readw(bus->mmio + offset);
 
 222         writew(value, bus->mmio + offset);
 
 225 /* Not "static", as it's used in main.c */
 
 226 const struct ssb_bus_ops ssb_pcmcia_ops = {
 
 227         .read16         = ssb_pcmcia_read16,
 
 228         .read32         = ssb_pcmcia_read32,
 
 229         .write16        = ssb_pcmcia_write16,
 
 230         .write32        = ssb_pcmcia_write32,
 
 233 int ssb_pcmcia_get_invariants(struct ssb_bus *bus,
 
 234                               struct ssb_init_invariants *iv)
 
 240 int ssb_pcmcia_init(struct ssb_bus *bus)
 
 245         if (bus->bustype != SSB_BUSTYPE_PCMCIA)
 
 248         /* Switch segment to a known state and sync
 
 249          * bus->mapped_pcmcia_seg with hardware state. */
 
 250         ssb_pcmcia_switch_segment(bus, 0);
 
 252         /* Init IRQ routing */
 
 253         reg.Action = CS_READ;
 
 255         if (bus->chip_id == 0x4306)
 
 259         err = pcmcia_access_configuration_register(bus->host_pcmcia, ®);
 
 260         if (err != CS_SUCCESS)
 
 262         reg.Action = CS_WRITE;
 
 263         reg.Value |= 0x04 | 0x01;
 
 264         err = pcmcia_access_configuration_register(bus->host_pcmcia, ®);
 
 265         if (err != CS_SUCCESS)