2 * Sonics Silicon Backplane
3 * PCMCIA-Hostbus related functions
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2007-2008 Michael Buesch <m@bues.ch>
8 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/ssb/ssb.h>
12 #include <linux/delay.h>
14 #include <linux/etherdevice.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 /* PCMCIA configuration registers */
29 #define SSB_PCMCIA_ADDRESS0 0x2E
30 #define SSB_PCMCIA_ADDRESS1 0x30
31 #define SSB_PCMCIA_ADDRESS2 0x32
32 #define SSB_PCMCIA_MEMSEG 0x34
33 #define SSB_PCMCIA_SPROMCTL 0x36
34 #define SSB_PCMCIA_SPROMCTL_IDLE 0
35 #define SSB_PCMCIA_SPROMCTL_WRITE 1
36 #define SSB_PCMCIA_SPROMCTL_READ 2
37 #define SSB_PCMCIA_SPROMCTL_WRITEEN 4
38 #define SSB_PCMCIA_SPROMCTL_WRITEDIS 7
39 #define SSB_PCMCIA_SPROMCTL_DONE 8
40 #define SSB_PCMCIA_SPROM_DATALO 0x38
41 #define SSB_PCMCIA_SPROM_DATAHI 0x3A
42 #define SSB_PCMCIA_SPROM_ADDRLO 0x3C
43 #define SSB_PCMCIA_SPROM_ADDRHI 0x3E
45 /* Hardware invariants CIS tuples */
46 #define SSB_PCMCIA_CIS 0x80
47 #define SSB_PCMCIA_CIS_ID 0x01
48 #define SSB_PCMCIA_CIS_BOARDREV 0x02
49 #define SSB_PCMCIA_CIS_PA 0x03
50 #define SSB_PCMCIA_CIS_PA_PA0B0_LO 0
51 #define SSB_PCMCIA_CIS_PA_PA0B0_HI 1
52 #define SSB_PCMCIA_CIS_PA_PA0B1_LO 2
53 #define SSB_PCMCIA_CIS_PA_PA0B1_HI 3
54 #define SSB_PCMCIA_CIS_PA_PA0B2_LO 4
55 #define SSB_PCMCIA_CIS_PA_PA0B2_HI 5
56 #define SSB_PCMCIA_CIS_PA_ITSSI 6
57 #define SSB_PCMCIA_CIS_PA_MAXPOW 7
58 #define SSB_PCMCIA_CIS_OEMNAME 0x04
59 #define SSB_PCMCIA_CIS_CCODE 0x05
60 #define SSB_PCMCIA_CIS_ANTENNA 0x06
61 #define SSB_PCMCIA_CIS_ANTGAIN 0x07
62 #define SSB_PCMCIA_CIS_BFLAGS 0x08
63 #define SSB_PCMCIA_CIS_LEDS 0x09
65 /* PCMCIA SPROM size. */
66 #define SSB_PCMCIA_SPROM_SIZE 256
67 #define SSB_PCMCIA_SPROM_SIZE_BYTES (SSB_PCMCIA_SPROM_SIZE * sizeof(u16))
70 /* Write to a PCMCIA configuration register. */
71 static int ssb_pcmcia_cfg_write(struct ssb_bus *bus, u8 offset, u8 value)
75 res = pcmcia_write_config_byte(bus->host_pcmcia, offset, value);
76 if (unlikely(res != 0))
82 /* Read from a PCMCIA configuration register. */
83 static int ssb_pcmcia_cfg_read(struct ssb_bus *bus, u8 offset, u8 *value)
87 res = pcmcia_read_config_byte(bus->host_pcmcia, offset, value);
88 if (unlikely(res != 0))
94 int ssb_pcmcia_switch_coreidx(struct ssb_bus *bus,
104 addr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
106 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS0,
107 (addr & 0x0000F000) >> 12);
110 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS1,
111 (addr & 0x00FF0000) >> 16);
114 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS2,
115 (addr & 0xFF000000) >> 24);
121 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS0, &val);
124 read_addr |= ((u32)(val & 0x0F)) << 12;
125 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS1, &val);
128 read_addr |= ((u32)val) << 16;
129 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS2, &val);
132 read_addr |= ((u32)val) << 24;
134 cur_core = (read_addr - SSB_ENUM_BASE) / SSB_CORE_SIZE;
135 if (cur_core == coreidx)
139 if (attempts++ > SSB_BAR0_MAX_RETRIES)
146 ssb_printk(KERN_ERR PFX "Failed to switch to core %u\n", coreidx);
150 int ssb_pcmcia_switch_core(struct ssb_bus *bus,
151 struct ssb_device *dev)
155 #if SSB_VERBOSE_PCMCIACORESWITCH_DEBUG
156 ssb_printk(KERN_INFO PFX
157 "Switching to %s core, index %d\n",
158 ssb_core_name(dev->id.coreid),
162 err = ssb_pcmcia_switch_coreidx(bus, dev->core_index);
164 bus->mapped_device = dev;
169 int ssb_pcmcia_switch_segment(struct ssb_bus *bus, u8 seg)
175 SSB_WARN_ON((seg != 0) && (seg != 1));
177 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_MEMSEG, seg);
180 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_MEMSEG, &val);
187 if (unlikely(attempts++ > SSB_BAR0_MAX_RETRIES))
191 bus->mapped_pcmcia_seg = seg;
195 ssb_printk(KERN_ERR PFX "Failed to switch pcmcia segment\n");
199 static int select_core_and_segment(struct ssb_device *dev,
202 struct ssb_bus *bus = dev->bus;
206 if (*offset >= 0x800) {
212 if (unlikely(dev != bus->mapped_device)) {
213 err = ssb_pcmcia_switch_core(bus, dev);
217 if (unlikely(need_segment != bus->mapped_pcmcia_seg)) {
218 err = ssb_pcmcia_switch_segment(bus, need_segment);
226 static u8 ssb_pcmcia_read8(struct ssb_device *dev, u16 offset)
228 struct ssb_bus *bus = dev->bus;
233 spin_lock_irqsave(&bus->bar_lock, flags);
234 err = select_core_and_segment(dev, &offset);
236 value = readb(bus->mmio + offset);
237 spin_unlock_irqrestore(&bus->bar_lock, flags);
242 static u16 ssb_pcmcia_read16(struct ssb_device *dev, u16 offset)
244 struct ssb_bus *bus = dev->bus;
249 spin_lock_irqsave(&bus->bar_lock, flags);
250 err = select_core_and_segment(dev, &offset);
252 value = readw(bus->mmio + offset);
253 spin_unlock_irqrestore(&bus->bar_lock, flags);
258 static u32 ssb_pcmcia_read32(struct ssb_device *dev, u16 offset)
260 struct ssb_bus *bus = dev->bus;
263 u32 lo = 0xFFFFFFFF, hi = 0xFFFFFFFF;
265 spin_lock_irqsave(&bus->bar_lock, flags);
266 err = select_core_and_segment(dev, &offset);
268 lo = readw(bus->mmio + offset);
269 hi = readw(bus->mmio + offset + 2);
271 spin_unlock_irqrestore(&bus->bar_lock, flags);
273 return (lo | (hi << 16));
276 #ifdef CONFIG_SSB_BLOCKIO
277 static void ssb_pcmcia_block_read(struct ssb_device *dev, void *buffer,
278 size_t count, u16 offset, u8 reg_width)
280 struct ssb_bus *bus = dev->bus;
282 void __iomem *addr = bus->mmio + offset;
285 spin_lock_irqsave(&bus->bar_lock, flags);
286 err = select_core_and_segment(dev, &offset);
288 memset(buffer, 0xFF, count);
296 *buf = __raw_readb(addr);
303 __le16 *buf = buffer;
305 SSB_WARN_ON(count & 1);
307 *buf = (__force __le16)__raw_readw(addr);
314 __le16 *buf = buffer;
316 SSB_WARN_ON(count & 3);
318 *buf = (__force __le16)__raw_readw(addr);
320 *buf = (__force __le16)__raw_readw(addr + 2);
330 spin_unlock_irqrestore(&bus->bar_lock, flags);
332 #endif /* CONFIG_SSB_BLOCKIO */
334 static void ssb_pcmcia_write8(struct ssb_device *dev, u16 offset, u8 value)
336 struct ssb_bus *bus = dev->bus;
340 spin_lock_irqsave(&bus->bar_lock, flags);
341 err = select_core_and_segment(dev, &offset);
343 writeb(value, bus->mmio + offset);
345 spin_unlock_irqrestore(&bus->bar_lock, flags);
348 static void ssb_pcmcia_write16(struct ssb_device *dev, u16 offset, u16 value)
350 struct ssb_bus *bus = dev->bus;
354 spin_lock_irqsave(&bus->bar_lock, flags);
355 err = select_core_and_segment(dev, &offset);
357 writew(value, bus->mmio + offset);
359 spin_unlock_irqrestore(&bus->bar_lock, flags);
362 static void ssb_pcmcia_write32(struct ssb_device *dev, u16 offset, u32 value)
364 struct ssb_bus *bus = dev->bus;
368 spin_lock_irqsave(&bus->bar_lock, flags);
369 err = select_core_and_segment(dev, &offset);
371 writew((value & 0x0000FFFF), bus->mmio + offset);
372 writew(((value & 0xFFFF0000) >> 16), bus->mmio + offset + 2);
375 spin_unlock_irqrestore(&bus->bar_lock, flags);
378 #ifdef CONFIG_SSB_BLOCKIO
379 static void ssb_pcmcia_block_write(struct ssb_device *dev, const void *buffer,
380 size_t count, u16 offset, u8 reg_width)
382 struct ssb_bus *bus = dev->bus;
384 void __iomem *addr = bus->mmio + offset;
387 spin_lock_irqsave(&bus->bar_lock, flags);
388 err = select_core_and_segment(dev, &offset);
393 const u8 *buf = buffer;
396 __raw_writeb(*buf, addr);
403 const __le16 *buf = buffer;
405 SSB_WARN_ON(count & 1);
407 __raw_writew((__force u16)(*buf), addr);
414 const __le16 *buf = buffer;
416 SSB_WARN_ON(count & 3);
418 __raw_writew((__force u16)(*buf), addr);
420 __raw_writew((__force u16)(*buf), addr + 2);
431 spin_unlock_irqrestore(&bus->bar_lock, flags);
433 #endif /* CONFIG_SSB_BLOCKIO */
435 /* Not "static", as it's used in main.c */
436 const struct ssb_bus_ops ssb_pcmcia_ops = {
437 .read8 = ssb_pcmcia_read8,
438 .read16 = ssb_pcmcia_read16,
439 .read32 = ssb_pcmcia_read32,
440 .write8 = ssb_pcmcia_write8,
441 .write16 = ssb_pcmcia_write16,
442 .write32 = ssb_pcmcia_write32,
443 #ifdef CONFIG_SSB_BLOCKIO
444 .block_read = ssb_pcmcia_block_read,
445 .block_write = ssb_pcmcia_block_write,
449 static int ssb_pcmcia_sprom_command(struct ssb_bus *bus, u8 command)
455 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROMCTL, command);
458 for (i = 0; i < 1000; i++) {
459 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROMCTL, &value);
462 if (value & SSB_PCMCIA_SPROMCTL_DONE)
470 /* offset is the 16bit word offset */
471 static int ssb_pcmcia_sprom_read(struct ssb_bus *bus, u16 offset, u16 *value)
476 offset *= 2; /* Make byte offset */
478 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO,
482 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI,
483 (offset & 0xFF00) >> 8);
486 err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_READ);
489 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATALO, &lo);
492 err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATAHI, &hi);
495 *value = (lo | (((u16)hi) << 8));
500 /* offset is the 16bit word offset */
501 static int ssb_pcmcia_sprom_write(struct ssb_bus *bus, u16 offset, u16 value)
505 offset *= 2; /* Make byte offset */
507 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO,
511 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI,
512 (offset & 0xFF00) >> 8);
515 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATALO,
519 err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATAHI,
520 (value & 0xFF00) >> 8);
523 err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITE);
531 /* Read the SPROM image. bufsize is in 16bit words. */
532 static int ssb_pcmcia_sprom_read_all(struct ssb_bus *bus, u16 *sprom)
536 for (i = 0; i < SSB_PCMCIA_SPROM_SIZE; i++) {
537 err = ssb_pcmcia_sprom_read(bus, i, &sprom[i]);
545 /* Write the SPROM image. size is in 16bit words. */
546 static int ssb_pcmcia_sprom_write_all(struct ssb_bus *bus, const u16 *sprom)
550 size_t size = SSB_PCMCIA_SPROM_SIZE;
552 ssb_printk(KERN_NOTICE PFX
553 "Writing SPROM. Do NOT turn off the power! "
554 "Please stand by...\n");
555 err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEEN);
557 ssb_printk(KERN_NOTICE PFX
558 "Could not enable SPROM write access.\n");
561 ssb_printk(KERN_NOTICE PFX "[ 0%%");
563 for (i = 0; i < size; i++) {
566 else if (i == size / 2)
568 else if (i == (size * 3) / 4)
572 err = ssb_pcmcia_sprom_write(bus, i, sprom[i]);
574 ssb_printk(KERN_NOTICE PFX
575 "Failed to write to SPROM.\n");
580 err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEDIS);
582 ssb_printk(KERN_NOTICE PFX
583 "Could not disable SPROM write access.\n");
588 ssb_printk("100%% ]\n");
589 ssb_printk(KERN_NOTICE PFX "SPROM written.\n");
592 return failed ? -EBUSY : 0;
595 static int ssb_pcmcia_sprom_check_crc(const u16 *sprom, size_t size)
601 #define GOTO_ERROR_ON(condition, description) do { \
602 if (unlikely(condition)) { \
603 error_description = description; \
608 static int ssb_pcmcia_get_mac(struct pcmcia_device *p_dev,
612 struct ssb_sprom *sprom = priv;
614 if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
616 if (tuple->TupleDataLen != ETH_ALEN + 2)
618 if (tuple->TupleData[1] != ETH_ALEN)
620 memcpy(sprom->il0mac, &tuple->TupleData[2], ETH_ALEN);
624 static int ssb_pcmcia_do_get_invariants(struct pcmcia_device *p_dev,
628 struct ssb_init_invariants *iv = priv;
629 struct ssb_sprom *sprom = &iv->sprom;
630 struct ssb_boardinfo *bi = &iv->boardinfo;
631 const char *error_description;
633 GOTO_ERROR_ON(tuple->TupleDataLen < 1, "VEN tpl < 1");
634 switch (tuple->TupleData[0]) {
635 case SSB_PCMCIA_CIS_ID:
636 GOTO_ERROR_ON((tuple->TupleDataLen != 5) &&
637 (tuple->TupleDataLen != 7),
639 bi->vendor = tuple->TupleData[1] |
640 ((u16)tuple->TupleData[2] << 8);
642 case SSB_PCMCIA_CIS_BOARDREV:
643 GOTO_ERROR_ON(tuple->TupleDataLen != 2,
644 "boardrev tpl size");
645 sprom->board_rev = tuple->TupleData[1];
647 case SSB_PCMCIA_CIS_PA:
648 GOTO_ERROR_ON((tuple->TupleDataLen != 9) &&
649 (tuple->TupleDataLen != 10),
651 sprom->pa0b0 = tuple->TupleData[1] |
652 ((u16)tuple->TupleData[2] << 8);
653 sprom->pa0b1 = tuple->TupleData[3] |
654 ((u16)tuple->TupleData[4] << 8);
655 sprom->pa0b2 = tuple->TupleData[5] |
656 ((u16)tuple->TupleData[6] << 8);
657 sprom->itssi_a = tuple->TupleData[7];
658 sprom->itssi_bg = tuple->TupleData[7];
659 sprom->maxpwr_a = tuple->TupleData[8];
660 sprom->maxpwr_bg = tuple->TupleData[8];
662 case SSB_PCMCIA_CIS_OEMNAME:
663 /* We ignore this. */
665 case SSB_PCMCIA_CIS_CCODE:
666 GOTO_ERROR_ON(tuple->TupleDataLen != 2,
668 sprom->country_code = tuple->TupleData[1];
670 case SSB_PCMCIA_CIS_ANTENNA:
671 GOTO_ERROR_ON(tuple->TupleDataLen != 2,
673 sprom->ant_available_a = tuple->TupleData[1];
674 sprom->ant_available_bg = tuple->TupleData[1];
676 case SSB_PCMCIA_CIS_ANTGAIN:
677 GOTO_ERROR_ON(tuple->TupleDataLen != 2,
679 sprom->antenna_gain.ghz24.a0 = tuple->TupleData[1];
680 sprom->antenna_gain.ghz24.a1 = tuple->TupleData[1];
681 sprom->antenna_gain.ghz24.a2 = tuple->TupleData[1];
682 sprom->antenna_gain.ghz24.a3 = tuple->TupleData[1];
683 sprom->antenna_gain.ghz5.a0 = tuple->TupleData[1];
684 sprom->antenna_gain.ghz5.a1 = tuple->TupleData[1];
685 sprom->antenna_gain.ghz5.a2 = tuple->TupleData[1];
686 sprom->antenna_gain.ghz5.a3 = tuple->TupleData[1];
688 case SSB_PCMCIA_CIS_BFLAGS:
689 GOTO_ERROR_ON((tuple->TupleDataLen != 3) &&
690 (tuple->TupleDataLen != 5),
692 sprom->boardflags_lo = tuple->TupleData[1] |
693 ((u16)tuple->TupleData[2] << 8);
695 case SSB_PCMCIA_CIS_LEDS:
696 GOTO_ERROR_ON(tuple->TupleDataLen != 5,
698 sprom->gpio0 = tuple->TupleData[1];
699 sprom->gpio1 = tuple->TupleData[2];
700 sprom->gpio2 = tuple->TupleData[3];
701 sprom->gpio3 = tuple->TupleData[4];
704 return -ENOSPC; /* continue with next entry */
707 ssb_printk(KERN_ERR PFX
708 "PCMCIA: Failed to fetch device invariants: %s\n",
714 int ssb_pcmcia_get_invariants(struct ssb_bus *bus,
715 struct ssb_init_invariants *iv)
717 struct ssb_sprom *sprom = &iv->sprom;
720 memset(sprom, 0xFF, sizeof(*sprom));
722 sprom->boardflags_lo = 0;
723 sprom->boardflags_hi = 0;
725 /* First fetch the MAC address. */
726 res = pcmcia_loop_tuple(bus->host_pcmcia, CISTPL_FUNCE,
727 ssb_pcmcia_get_mac, sprom);
729 ssb_printk(KERN_ERR PFX
730 "PCMCIA: Failed to fetch MAC address\n");
734 /* Fetch the vendor specific tuples. */
735 res = pcmcia_loop_tuple(bus->host_pcmcia, SSB_PCMCIA_CIS,
736 ssb_pcmcia_do_get_invariants, iv);
737 if ((res == 0) || (res == -ENOSPC))
740 ssb_printk(KERN_ERR PFX
741 "PCMCIA: Failed to fetch device invariants\n");
745 static ssize_t ssb_pcmcia_attr_sprom_show(struct device *pcmciadev,
746 struct device_attribute *attr,
749 struct pcmcia_device *pdev =
750 container_of(pcmciadev, struct pcmcia_device, dev);
753 bus = ssb_pcmcia_dev_to_bus(pdev);
757 return ssb_attr_sprom_show(bus, buf,
758 ssb_pcmcia_sprom_read_all);
761 static ssize_t ssb_pcmcia_attr_sprom_store(struct device *pcmciadev,
762 struct device_attribute *attr,
763 const char *buf, size_t count)
765 struct pcmcia_device *pdev =
766 container_of(pcmciadev, struct pcmcia_device, dev);
769 bus = ssb_pcmcia_dev_to_bus(pdev);
773 return ssb_attr_sprom_store(bus, buf, count,
774 ssb_pcmcia_sprom_check_crc,
775 ssb_pcmcia_sprom_write_all);
778 static DEVICE_ATTR(ssb_sprom, 0600,
779 ssb_pcmcia_attr_sprom_show,
780 ssb_pcmcia_attr_sprom_store);
782 static int ssb_pcmcia_cor_setup(struct ssb_bus *bus, u8 cor)
787 err = ssb_pcmcia_cfg_read(bus, cor, &val);
790 val &= ~COR_SOFT_RESET;
791 val |= COR_FUNC_ENA | COR_IREQ_ENA | COR_LEVEL_REQ;
792 err = ssb_pcmcia_cfg_write(bus, cor, val);
800 /* Initialize the PCMCIA hardware. This is called on Init and Resume. */
801 int ssb_pcmcia_hardware_setup(struct ssb_bus *bus)
805 if (bus->bustype != SSB_BUSTYPE_PCMCIA)
808 /* Switch segment to a known state and sync
809 * bus->mapped_pcmcia_seg with hardware state. */
810 ssb_pcmcia_switch_segment(bus, 0);
811 /* Init the COR register. */
812 err = ssb_pcmcia_cor_setup(bus, CISREG_COR);
815 /* Some cards also need this register to get poked. */
816 err = ssb_pcmcia_cor_setup(bus, CISREG_COR + 0x80);
823 void ssb_pcmcia_exit(struct ssb_bus *bus)
825 if (bus->bustype != SSB_BUSTYPE_PCMCIA)
828 device_remove_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom);
831 int ssb_pcmcia_init(struct ssb_bus *bus)
835 if (bus->bustype != SSB_BUSTYPE_PCMCIA)
838 err = ssb_pcmcia_hardware_setup(bus);
842 bus->sprom_size = SSB_PCMCIA_SPROM_SIZE;
843 mutex_init(&bus->sprom_mutex);
844 err = device_create_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom);
850 ssb_printk(KERN_ERR PFX "Failed to initialize PCMCIA host device\n");