Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Sonics Silicon Backplane |
| 3 | * PCMCIA-Hostbus related functions |
| 4 | * |
| 5 | * Copyright 2006 Johannes Berg <johannes@sipsolutions.net> |
Michael Büsch | eb032b9 | 2011-07-04 20:50:05 +0200 | [diff] [blame] | 6 | * Copyright 2007-2008 Michael Buesch <m@bues.ch> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 7 | * |
| 8 | * Licensed under the GNU/GPL. See COPYING for details. |
| 9 | */ |
| 10 | |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 11 | #include "ssb_private.h" |
| 12 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 13 | #include <linux/ssb/ssb.h> |
| 14 | #include <linux/delay.h> |
Al Viro | 409f243 | 2007-10-14 05:51:51 +0100 | [diff] [blame] | 15 | #include <linux/io.h> |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 16 | #include <linux/etherdevice.h> |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 17 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 18 | #include <pcmcia/cistpl.h> |
| 19 | #include <pcmcia/ciscode.h> |
| 20 | #include <pcmcia/ds.h> |
| 21 | #include <pcmcia/cisreg.h> |
| 22 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 23 | |
| 24 | /* Define the following to 1 to enable a printk on each coreswitch. */ |
| 25 | #define SSB_VERBOSE_PCMCIACORESWITCH_DEBUG 0 |
| 26 | |
| 27 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 28 | /* PCMCIA configuration registers */ |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 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 |
| 44 | |
| 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 |
| 64 | |
| 65 | /* PCMCIA SPROM size. */ |
| 66 | #define SSB_PCMCIA_SPROM_SIZE 256 |
| 67 | #define SSB_PCMCIA_SPROM_SIZE_BYTES (SSB_PCMCIA_SPROM_SIZE * sizeof(u16)) |
| 68 | |
| 69 | |
| 70 | /* Write to a PCMCIA configuration register. */ |
| 71 | static int ssb_pcmcia_cfg_write(struct ssb_bus *bus, u8 offset, u8 value) |
| 72 | { |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 73 | int res; |
| 74 | |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 75 | res = pcmcia_write_config_byte(bus->host_pcmcia, offset, value); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 76 | if (unlikely(res != 0)) |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 77 | return -EBUSY; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* Read from a PCMCIA configuration register. */ |
| 83 | static int ssb_pcmcia_cfg_read(struct ssb_bus *bus, u8 offset, u8 *value) |
| 84 | { |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 85 | int res; |
| 86 | |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 87 | res = pcmcia_read_config_byte(bus->host_pcmcia, offset, value); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 88 | if (unlikely(res != 0)) |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 89 | return -EBUSY; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 94 | int ssb_pcmcia_switch_coreidx(struct ssb_bus *bus, |
| 95 | u8 coreidx) |
| 96 | { |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 97 | int err; |
| 98 | int attempts = 0; |
| 99 | u32 cur_core; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 100 | u32 addr; |
| 101 | u32 read_addr; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 102 | u8 val; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 103 | |
| 104 | addr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE; |
| 105 | while (1) { |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 106 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS0, |
| 107 | (addr & 0x0000F000) >> 12); |
| 108 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 109 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 110 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS1, |
| 111 | (addr & 0x00FF0000) >> 16); |
| 112 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 113 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 114 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS2, |
| 115 | (addr & 0xFF000000) >> 24); |
| 116 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 117 | goto error; |
| 118 | |
| 119 | read_addr = 0; |
| 120 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 121 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS0, &val); |
| 122 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 123 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 124 | read_addr |= ((u32)(val & 0x0F)) << 12; |
| 125 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS1, &val); |
| 126 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 127 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 128 | read_addr |= ((u32)val) << 16; |
| 129 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS2, &val); |
| 130 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 131 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 132 | read_addr |= ((u32)val) << 24; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 133 | |
| 134 | cur_core = (read_addr - SSB_ENUM_BASE) / SSB_CORE_SIZE; |
| 135 | if (cur_core == coreidx) |
| 136 | break; |
| 137 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 138 | err = -ETIMEDOUT; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 139 | if (attempts++ > SSB_BAR0_MAX_RETRIES) |
| 140 | goto error; |
| 141 | udelay(10); |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | error: |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 146 | pr_err("Failed to switch to core %u\n", coreidx); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 147 | return err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Rafał Miłecki | cf75496 | 2015-09-21 09:55:26 +0200 | [diff] [blame] | 150 | static int ssb_pcmcia_switch_core(struct ssb_bus *bus, struct ssb_device *dev) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 151 | { |
| 152 | int err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 153 | |
| 154 | #if SSB_VERBOSE_PCMCIACORESWITCH_DEBUG |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 155 | pr_info("Switching to %s core, index %d\n", |
| 156 | ssb_core_name(dev->id.coreid), dev->core_index); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 157 | #endif |
| 158 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 159 | err = ssb_pcmcia_switch_coreidx(bus, dev->core_index); |
| 160 | if (!err) |
| 161 | bus->mapped_device = dev; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 162 | |
| 163 | return err; |
| 164 | } |
| 165 | |
| 166 | int ssb_pcmcia_switch_segment(struct ssb_bus *bus, u8 seg) |
| 167 | { |
| 168 | int attempts = 0; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 169 | int err; |
| 170 | u8 val; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 171 | |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 172 | WARN_ON((seg != 0) && (seg != 1)); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 173 | while (1) { |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 174 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_MEMSEG, seg); |
| 175 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 176 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 177 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_MEMSEG, &val); |
| 178 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 179 | goto error; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 180 | if (val == seg) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 181 | break; |
| 182 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 183 | err = -ETIMEDOUT; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 184 | if (unlikely(attempts++ > SSB_BAR0_MAX_RETRIES)) |
| 185 | goto error; |
| 186 | udelay(10); |
| 187 | } |
| 188 | bus->mapped_pcmcia_seg = seg; |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 189 | |
| 190 | return 0; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 191 | error: |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 192 | pr_err("Failed to switch pcmcia segment\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 193 | return err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Michael Buesch | 60d78c4 | 2007-11-07 19:03:35 +0100 | [diff] [blame] | 196 | static int select_core_and_segment(struct ssb_device *dev, |
| 197 | u16 *offset) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 198 | { |
Michael Buesch | 60d78c4 | 2007-11-07 19:03:35 +0100 | [diff] [blame] | 199 | struct ssb_bus *bus = dev->bus; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 200 | int err; |
Michael Buesch | 60d78c4 | 2007-11-07 19:03:35 +0100 | [diff] [blame] | 201 | u8 need_segment; |
| 202 | |
| 203 | if (*offset >= 0x800) { |
| 204 | *offset -= 0x800; |
| 205 | need_segment = 1; |
| 206 | } else |
| 207 | need_segment = 0; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 208 | |
| 209 | if (unlikely(dev != bus->mapped_device)) { |
| 210 | err = ssb_pcmcia_switch_core(bus, dev); |
| 211 | if (unlikely(err)) |
| 212 | return err; |
| 213 | } |
Michael Buesch | 60d78c4 | 2007-11-07 19:03:35 +0100 | [diff] [blame] | 214 | if (unlikely(need_segment != bus->mapped_pcmcia_seg)) { |
| 215 | err = ssb_pcmcia_switch_segment(bus, need_segment); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 216 | if (unlikely(err)) |
| 217 | return err; |
| 218 | } |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 223 | static u8 ssb_pcmcia_read8(struct ssb_device *dev, u16 offset) |
| 224 | { |
| 225 | struct ssb_bus *bus = dev->bus; |
| 226 | unsigned long flags; |
| 227 | int err; |
| 228 | u8 value = 0xFF; |
| 229 | |
| 230 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 231 | err = select_core_and_segment(dev, &offset); |
| 232 | if (likely(!err)) |
| 233 | value = readb(bus->mmio + offset); |
| 234 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
| 235 | |
| 236 | return value; |
| 237 | } |
| 238 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 239 | static u16 ssb_pcmcia_read16(struct ssb_device *dev, u16 offset) |
| 240 | { |
| 241 | struct ssb_bus *bus = dev->bus; |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 242 | unsigned long flags; |
| 243 | int err; |
| 244 | u16 value = 0xFFFF; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 245 | |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 246 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 247 | err = select_core_and_segment(dev, &offset); |
| 248 | if (likely(!err)) |
| 249 | value = readw(bus->mmio + offset); |
| 250 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 251 | |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 252 | return value; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static u32 ssb_pcmcia_read32(struct ssb_device *dev, u16 offset) |
| 256 | { |
| 257 | struct ssb_bus *bus = dev->bus; |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 258 | unsigned long flags; |
| 259 | int err; |
| 260 | u32 lo = 0xFFFFFFFF, hi = 0xFFFFFFFF; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 261 | |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 262 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 263 | err = select_core_and_segment(dev, &offset); |
| 264 | if (likely(!err)) { |
| 265 | lo = readw(bus->mmio + offset); |
| 266 | hi = readw(bus->mmio + offset + 2); |
| 267 | } |
| 268 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 269 | |
Michael Buesch | 60d78c4 | 2007-11-07 19:03:35 +0100 | [diff] [blame] | 270 | return (lo | (hi << 16)); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 273 | #ifdef CONFIG_SSB_BLOCKIO |
| 274 | static void ssb_pcmcia_block_read(struct ssb_device *dev, void *buffer, |
| 275 | size_t count, u16 offset, u8 reg_width) |
| 276 | { |
| 277 | struct ssb_bus *bus = dev->bus; |
| 278 | unsigned long flags; |
| 279 | void __iomem *addr = bus->mmio + offset; |
| 280 | int err; |
| 281 | |
| 282 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 283 | err = select_core_and_segment(dev, &offset); |
| 284 | if (unlikely(err)) { |
| 285 | memset(buffer, 0xFF, count); |
| 286 | goto unlock; |
| 287 | } |
| 288 | switch (reg_width) { |
| 289 | case sizeof(u8): { |
| 290 | u8 *buf = buffer; |
| 291 | |
| 292 | while (count) { |
| 293 | *buf = __raw_readb(addr); |
| 294 | buf++; |
| 295 | count--; |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | case sizeof(u16): { |
| 300 | __le16 *buf = buffer; |
| 301 | |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 302 | WARN_ON(count & 1); |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 303 | while (count) { |
| 304 | *buf = (__force __le16)__raw_readw(addr); |
| 305 | buf++; |
| 306 | count -= 2; |
| 307 | } |
| 308 | break; |
| 309 | } |
| 310 | case sizeof(u32): { |
| 311 | __le16 *buf = buffer; |
| 312 | |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 313 | WARN_ON(count & 3); |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 314 | while (count) { |
| 315 | *buf = (__force __le16)__raw_readw(addr); |
| 316 | buf++; |
| 317 | *buf = (__force __le16)__raw_readw(addr + 2); |
| 318 | buf++; |
| 319 | count -= 4; |
| 320 | } |
| 321 | break; |
| 322 | } |
| 323 | default: |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 324 | WARN_ON(1); |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 325 | } |
| 326 | unlock: |
| 327 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
| 328 | } |
| 329 | #endif /* CONFIG_SSB_BLOCKIO */ |
| 330 | |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 331 | static void ssb_pcmcia_write8(struct ssb_device *dev, u16 offset, u8 value) |
| 332 | { |
| 333 | struct ssb_bus *bus = dev->bus; |
| 334 | unsigned long flags; |
| 335 | int err; |
| 336 | |
| 337 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 338 | err = select_core_and_segment(dev, &offset); |
| 339 | if (likely(!err)) |
| 340 | writeb(value, bus->mmio + offset); |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 341 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
| 342 | } |
| 343 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 344 | static void ssb_pcmcia_write16(struct ssb_device *dev, u16 offset, u16 value) |
| 345 | { |
| 346 | struct ssb_bus *bus = dev->bus; |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 347 | unsigned long flags; |
| 348 | int err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 349 | |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 350 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 351 | err = select_core_and_segment(dev, &offset); |
| 352 | if (likely(!err)) |
| 353 | writew(value, bus->mmio + offset); |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 354 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static void ssb_pcmcia_write32(struct ssb_device *dev, u16 offset, u32 value) |
| 358 | { |
| 359 | struct ssb_bus *bus = dev->bus; |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 360 | unsigned long flags; |
| 361 | int err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 362 | |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 363 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 364 | err = select_core_and_segment(dev, &offset); |
| 365 | if (likely(!err)) { |
| 366 | writew((value & 0x0000FFFF), bus->mmio + offset); |
| 367 | writew(((value & 0xFFFF0000) >> 16), bus->mmio + offset + 2); |
| 368 | } |
Michael Buesch | 993e1c7 | 2007-12-22 22:01:36 +0100 | [diff] [blame] | 369 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 372 | #ifdef CONFIG_SSB_BLOCKIO |
| 373 | static void ssb_pcmcia_block_write(struct ssb_device *dev, const void *buffer, |
| 374 | size_t count, u16 offset, u8 reg_width) |
| 375 | { |
| 376 | struct ssb_bus *bus = dev->bus; |
| 377 | unsigned long flags; |
| 378 | void __iomem *addr = bus->mmio + offset; |
| 379 | int err; |
| 380 | |
| 381 | spin_lock_irqsave(&bus->bar_lock, flags); |
| 382 | err = select_core_and_segment(dev, &offset); |
| 383 | if (unlikely(err)) |
| 384 | goto unlock; |
| 385 | switch (reg_width) { |
| 386 | case sizeof(u8): { |
| 387 | const u8 *buf = buffer; |
| 388 | |
| 389 | while (count) { |
| 390 | __raw_writeb(*buf, addr); |
| 391 | buf++; |
| 392 | count--; |
| 393 | } |
| 394 | break; |
| 395 | } |
| 396 | case sizeof(u16): { |
| 397 | const __le16 *buf = buffer; |
| 398 | |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 399 | WARN_ON(count & 1); |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 400 | while (count) { |
| 401 | __raw_writew((__force u16)(*buf), addr); |
| 402 | buf++; |
| 403 | count -= 2; |
| 404 | } |
| 405 | break; |
| 406 | } |
| 407 | case sizeof(u32): { |
| 408 | const __le16 *buf = buffer; |
| 409 | |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 410 | WARN_ON(count & 3); |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 411 | while (count) { |
| 412 | __raw_writew((__force u16)(*buf), addr); |
| 413 | buf++; |
| 414 | __raw_writew((__force u16)(*buf), addr + 2); |
| 415 | buf++; |
| 416 | count -= 4; |
| 417 | } |
| 418 | break; |
| 419 | } |
| 420 | default: |
Michael Büsch | 209b437 | 2018-07-31 22:15:09 +0200 | [diff] [blame] | 421 | WARN_ON(1); |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 422 | } |
| 423 | unlock: |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 424 | spin_unlock_irqrestore(&bus->bar_lock, flags); |
| 425 | } |
| 426 | #endif /* CONFIG_SSB_BLOCKIO */ |
| 427 | |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 428 | /* Not "static", as it's used in main.c */ |
| 429 | const struct ssb_bus_ops ssb_pcmcia_ops = { |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 430 | .read8 = ssb_pcmcia_read8, |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 431 | .read16 = ssb_pcmcia_read16, |
| 432 | .read32 = ssb_pcmcia_read32, |
Michael Buesch | ffc7689 | 2008-02-20 19:08:10 +0100 | [diff] [blame] | 433 | .write8 = ssb_pcmcia_write8, |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 434 | .write16 = ssb_pcmcia_write16, |
| 435 | .write32 = ssb_pcmcia_write32, |
Michael Buesch | d625a29 | 2008-04-02 19:46:56 +0200 | [diff] [blame] | 436 | #ifdef CONFIG_SSB_BLOCKIO |
| 437 | .block_read = ssb_pcmcia_block_read, |
| 438 | .block_write = ssb_pcmcia_block_write, |
| 439 | #endif |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 440 | }; |
| 441 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 442 | static int ssb_pcmcia_sprom_command(struct ssb_bus *bus, u8 command) |
| 443 | { |
| 444 | unsigned int i; |
| 445 | int err; |
| 446 | u8 value; |
| 447 | |
| 448 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROMCTL, command); |
| 449 | if (err) |
| 450 | return err; |
| 451 | for (i = 0; i < 1000; i++) { |
| 452 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROMCTL, &value); |
| 453 | if (err) |
| 454 | return err; |
| 455 | if (value & SSB_PCMCIA_SPROMCTL_DONE) |
| 456 | return 0; |
| 457 | udelay(10); |
| 458 | } |
| 459 | |
| 460 | return -ETIMEDOUT; |
| 461 | } |
| 462 | |
| 463 | /* offset is the 16bit word offset */ |
| 464 | static int ssb_pcmcia_sprom_read(struct ssb_bus *bus, u16 offset, u16 *value) |
| 465 | { |
| 466 | int err; |
| 467 | u8 lo, hi; |
| 468 | |
| 469 | offset *= 2; /* Make byte offset */ |
| 470 | |
| 471 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO, |
| 472 | (offset & 0x00FF)); |
| 473 | if (err) |
| 474 | return err; |
| 475 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI, |
| 476 | (offset & 0xFF00) >> 8); |
| 477 | if (err) |
| 478 | return err; |
| 479 | err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_READ); |
| 480 | if (err) |
| 481 | return err; |
| 482 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATALO, &lo); |
| 483 | if (err) |
| 484 | return err; |
| 485 | err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATAHI, &hi); |
| 486 | if (err) |
| 487 | return err; |
| 488 | *value = (lo | (((u16)hi) << 8)); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /* offset is the 16bit word offset */ |
| 494 | static int ssb_pcmcia_sprom_write(struct ssb_bus *bus, u16 offset, u16 value) |
| 495 | { |
| 496 | int err; |
| 497 | |
| 498 | offset *= 2; /* Make byte offset */ |
| 499 | |
| 500 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO, |
| 501 | (offset & 0x00FF)); |
| 502 | if (err) |
| 503 | return err; |
| 504 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI, |
| 505 | (offset & 0xFF00) >> 8); |
| 506 | if (err) |
| 507 | return err; |
| 508 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATALO, |
| 509 | (value & 0x00FF)); |
| 510 | if (err) |
| 511 | return err; |
| 512 | err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATAHI, |
| 513 | (value & 0xFF00) >> 8); |
| 514 | if (err) |
| 515 | return err; |
| 516 | err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITE); |
| 517 | if (err) |
| 518 | return err; |
| 519 | msleep(20); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | /* Read the SPROM image. bufsize is in 16bit words. */ |
| 525 | static int ssb_pcmcia_sprom_read_all(struct ssb_bus *bus, u16 *sprom) |
| 526 | { |
| 527 | int err, i; |
| 528 | |
| 529 | for (i = 0; i < SSB_PCMCIA_SPROM_SIZE; i++) { |
| 530 | err = ssb_pcmcia_sprom_read(bus, i, &sprom[i]); |
| 531 | if (err) |
| 532 | return err; |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | /* Write the SPROM image. size is in 16bit words. */ |
| 539 | static int ssb_pcmcia_sprom_write_all(struct ssb_bus *bus, const u16 *sprom) |
| 540 | { |
| 541 | int i, err; |
| 542 | bool failed = 0; |
| 543 | size_t size = SSB_PCMCIA_SPROM_SIZE; |
| 544 | |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 545 | pr_notice("Writing SPROM. Do NOT turn off the power! Please stand by...\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 546 | err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEEN); |
| 547 | if (err) { |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 548 | pr_notice("Could not enable SPROM write access\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 549 | return -EBUSY; |
| 550 | } |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 551 | pr_notice("[ 0%%"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 552 | msleep(500); |
| 553 | for (i = 0; i < size; i++) { |
| 554 | if (i == size / 4) |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 555 | pr_cont("25%%"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 556 | else if (i == size / 2) |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 557 | pr_cont("50%%"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 558 | else if (i == (size * 3) / 4) |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 559 | pr_cont("75%%"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 560 | else if (i % 2) |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 561 | pr_cont("."); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 562 | err = ssb_pcmcia_sprom_write(bus, i, sprom[i]); |
| 563 | if (err) { |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 564 | pr_notice("Failed to write to SPROM\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 565 | failed = 1; |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEDIS); |
| 570 | if (err) { |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 571 | pr_notice("Could not disable SPROM write access\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 572 | failed = 1; |
| 573 | } |
| 574 | msleep(500); |
| 575 | if (!failed) { |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 576 | pr_cont("100%% ]\n"); |
| 577 | pr_notice("SPROM written\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | return failed ? -EBUSY : 0; |
| 581 | } |
| 582 | |
| 583 | static int ssb_pcmcia_sprom_check_crc(const u16 *sprom, size_t size) |
| 584 | { |
| 585 | //TODO |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | #define GOTO_ERROR_ON(condition, description) do { \ |
| 590 | if (unlikely(condition)) { \ |
| 591 | error_description = description; \ |
| 592 | goto error; \ |
| 593 | } \ |
| 594 | } while (0) |
| 595 | |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 596 | static int ssb_pcmcia_get_mac(struct pcmcia_device *p_dev, |
| 597 | tuple_t *tuple, |
| 598 | void *priv) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 599 | { |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 600 | struct ssb_sprom *sprom = priv; |
| 601 | |
| 602 | if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID) |
| 603 | return -EINVAL; |
| 604 | if (tuple->TupleDataLen != ETH_ALEN + 2) |
| 605 | return -EINVAL; |
| 606 | if (tuple->TupleData[1] != ETH_ALEN) |
| 607 | return -EINVAL; |
| 608 | memcpy(sprom->il0mac, &tuple->TupleData[2], ETH_ALEN); |
| 609 | return 0; |
| 610 | }; |
| 611 | |
| 612 | static int ssb_pcmcia_do_get_invariants(struct pcmcia_device *p_dev, |
| 613 | tuple_t *tuple, |
| 614 | void *priv) |
| 615 | { |
| 616 | struct ssb_init_invariants *iv = priv; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 617 | struct ssb_sprom *sprom = &iv->sprom; |
| 618 | struct ssb_boardinfo *bi = &iv->boardinfo; |
| 619 | const char *error_description; |
| 620 | |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 621 | GOTO_ERROR_ON(tuple->TupleDataLen < 1, "VEN tpl < 1"); |
| 622 | switch (tuple->TupleData[0]) { |
| 623 | case SSB_PCMCIA_CIS_ID: |
| 624 | GOTO_ERROR_ON((tuple->TupleDataLen != 5) && |
| 625 | (tuple->TupleDataLen != 7), |
| 626 | "id tpl size"); |
| 627 | bi->vendor = tuple->TupleData[1] | |
| 628 | ((u16)tuple->TupleData[2] << 8); |
| 629 | break; |
| 630 | case SSB_PCMCIA_CIS_BOARDREV: |
| 631 | GOTO_ERROR_ON(tuple->TupleDataLen != 2, |
| 632 | "boardrev tpl size"); |
| 633 | sprom->board_rev = tuple->TupleData[1]; |
| 634 | break; |
| 635 | case SSB_PCMCIA_CIS_PA: |
| 636 | GOTO_ERROR_ON((tuple->TupleDataLen != 9) && |
| 637 | (tuple->TupleDataLen != 10), |
| 638 | "pa tpl size"); |
| 639 | sprom->pa0b0 = tuple->TupleData[1] | |
| 640 | ((u16)tuple->TupleData[2] << 8); |
| 641 | sprom->pa0b1 = tuple->TupleData[3] | |
| 642 | ((u16)tuple->TupleData[4] << 8); |
| 643 | sprom->pa0b2 = tuple->TupleData[5] | |
| 644 | ((u16)tuple->TupleData[6] << 8); |
| 645 | sprom->itssi_a = tuple->TupleData[7]; |
| 646 | sprom->itssi_bg = tuple->TupleData[7]; |
| 647 | sprom->maxpwr_a = tuple->TupleData[8]; |
| 648 | sprom->maxpwr_bg = tuple->TupleData[8]; |
| 649 | break; |
| 650 | case SSB_PCMCIA_CIS_OEMNAME: |
| 651 | /* We ignore this. */ |
| 652 | break; |
| 653 | case SSB_PCMCIA_CIS_CCODE: |
| 654 | GOTO_ERROR_ON(tuple->TupleDataLen != 2, |
| 655 | "ccode tpl size"); |
| 656 | sprom->country_code = tuple->TupleData[1]; |
| 657 | break; |
| 658 | case SSB_PCMCIA_CIS_ANTENNA: |
| 659 | GOTO_ERROR_ON(tuple->TupleDataLen != 2, |
| 660 | "ant tpl size"); |
| 661 | sprom->ant_available_a = tuple->TupleData[1]; |
| 662 | sprom->ant_available_bg = tuple->TupleData[1]; |
| 663 | break; |
| 664 | case SSB_PCMCIA_CIS_ANTGAIN: |
| 665 | GOTO_ERROR_ON(tuple->TupleDataLen != 2, |
| 666 | "antg tpl size"); |
Hauke Mehrtens | f8f8a66 | 2012-02-28 00:56:05 +0100 | [diff] [blame] | 667 | sprom->antenna_gain.a0 = tuple->TupleData[1]; |
| 668 | sprom->antenna_gain.a1 = tuple->TupleData[1]; |
| 669 | sprom->antenna_gain.a2 = tuple->TupleData[1]; |
| 670 | sprom->antenna_gain.a3 = tuple->TupleData[1]; |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 671 | break; |
| 672 | case SSB_PCMCIA_CIS_BFLAGS: |
| 673 | GOTO_ERROR_ON((tuple->TupleDataLen != 3) && |
| 674 | (tuple->TupleDataLen != 5), |
| 675 | "bfl tpl size"); |
| 676 | sprom->boardflags_lo = tuple->TupleData[1] | |
| 677 | ((u16)tuple->TupleData[2] << 8); |
| 678 | break; |
| 679 | case SSB_PCMCIA_CIS_LEDS: |
| 680 | GOTO_ERROR_ON(tuple->TupleDataLen != 5, |
| 681 | "leds tpl size"); |
| 682 | sprom->gpio0 = tuple->TupleData[1]; |
| 683 | sprom->gpio1 = tuple->TupleData[2]; |
| 684 | sprom->gpio2 = tuple->TupleData[3]; |
| 685 | sprom->gpio3 = tuple->TupleData[4]; |
| 686 | break; |
| 687 | } |
| 688 | return -ENOSPC; /* continue with next entry */ |
| 689 | |
| 690 | error: |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 691 | pr_err("PCMCIA: Failed to fetch device invariants: %s\n", |
| 692 | error_description); |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 693 | return -ENODEV; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | int ssb_pcmcia_get_invariants(struct ssb_bus *bus, |
| 698 | struct ssb_init_invariants *iv) |
| 699 | { |
| 700 | struct ssb_sprom *sprom = &iv->sprom; |
| 701 | int res; |
| 702 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 703 | memset(sprom, 0xFF, sizeof(*sprom)); |
| 704 | sprom->revision = 1; |
| 705 | sprom->boardflags_lo = 0; |
| 706 | sprom->boardflags_hi = 0; |
| 707 | |
| 708 | /* First fetch the MAC address. */ |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 709 | res = pcmcia_loop_tuple(bus->host_pcmcia, CISTPL_FUNCE, |
| 710 | ssb_pcmcia_get_mac, sprom); |
| 711 | if (res != 0) { |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 712 | pr_err("PCMCIA: Failed to fetch MAC address\n"); |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 713 | return -ENODEV; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 714 | } |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 715 | |
| 716 | /* Fetch the vendor specific tuples. */ |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 717 | res = pcmcia_loop_tuple(bus->host_pcmcia, SSB_PCMCIA_CIS, |
Michael Büsch | dd3cb63 | 2011-02-04 23:34:45 +0100 | [diff] [blame] | 718 | ssb_pcmcia_do_get_invariants, iv); |
Dominik Brodowski | 37ace3d | 2009-10-18 23:56:41 +0200 | [diff] [blame] | 719 | if ((res == 0) || (res == -ENOSPC)) |
| 720 | return 0; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 721 | |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 722 | pr_err("PCMCIA: Failed to fetch device invariants\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 723 | return -ENODEV; |
| 724 | } |
| 725 | |
| 726 | static ssize_t ssb_pcmcia_attr_sprom_show(struct device *pcmciadev, |
| 727 | struct device_attribute *attr, |
| 728 | char *buf) |
| 729 | { |
| 730 | struct pcmcia_device *pdev = |
| 731 | container_of(pcmciadev, struct pcmcia_device, dev); |
| 732 | struct ssb_bus *bus; |
| 733 | |
| 734 | bus = ssb_pcmcia_dev_to_bus(pdev); |
| 735 | if (!bus) |
| 736 | return -ENODEV; |
| 737 | |
| 738 | return ssb_attr_sprom_show(bus, buf, |
| 739 | ssb_pcmcia_sprom_read_all); |
| 740 | } |
| 741 | |
| 742 | static ssize_t ssb_pcmcia_attr_sprom_store(struct device *pcmciadev, |
| 743 | struct device_attribute *attr, |
| 744 | const char *buf, size_t count) |
| 745 | { |
| 746 | struct pcmcia_device *pdev = |
| 747 | container_of(pcmciadev, struct pcmcia_device, dev); |
| 748 | struct ssb_bus *bus; |
| 749 | |
| 750 | bus = ssb_pcmcia_dev_to_bus(pdev); |
| 751 | if (!bus) |
| 752 | return -ENODEV; |
| 753 | |
| 754 | return ssb_attr_sprom_store(bus, buf, count, |
| 755 | ssb_pcmcia_sprom_check_crc, |
| 756 | ssb_pcmcia_sprom_write_all); |
| 757 | } |
| 758 | |
| 759 | static DEVICE_ATTR(ssb_sprom, 0600, |
| 760 | ssb_pcmcia_attr_sprom_show, |
| 761 | ssb_pcmcia_attr_sprom_store); |
| 762 | |
Michael Buesch | 9788ba7 | 2008-03-28 10:34:55 +0100 | [diff] [blame] | 763 | static int ssb_pcmcia_cor_setup(struct ssb_bus *bus, u8 cor) |
| 764 | { |
| 765 | u8 val; |
| 766 | int err; |
| 767 | |
| 768 | err = ssb_pcmcia_cfg_read(bus, cor, &val); |
| 769 | if (err) |
| 770 | return err; |
| 771 | val &= ~COR_SOFT_RESET; |
| 772 | val |= COR_FUNC_ENA | COR_IREQ_ENA | COR_LEVEL_REQ; |
| 773 | err = ssb_pcmcia_cfg_write(bus, cor, val); |
| 774 | if (err) |
| 775 | return err; |
| 776 | msleep(40); |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Michael Buesch | 8fe2b65 | 2008-03-30 00:10:50 +0100 | [diff] [blame] | 781 | /* Initialize the PCMCIA hardware. This is called on Init and Resume. */ |
| 782 | int ssb_pcmcia_hardware_setup(struct ssb_bus *bus) |
| 783 | { |
| 784 | int err; |
| 785 | |
| 786 | if (bus->bustype != SSB_BUSTYPE_PCMCIA) |
| 787 | return 0; |
| 788 | |
| 789 | /* Switch segment to a known state and sync |
| 790 | * bus->mapped_pcmcia_seg with hardware state. */ |
| 791 | ssb_pcmcia_switch_segment(bus, 0); |
| 792 | /* Init the COR register. */ |
| 793 | err = ssb_pcmcia_cor_setup(bus, CISREG_COR); |
| 794 | if (err) |
| 795 | return err; |
| 796 | /* Some cards also need this register to get poked. */ |
| 797 | err = ssb_pcmcia_cor_setup(bus, CISREG_COR + 0x80); |
| 798 | if (err) |
| 799 | return err; |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 804 | void ssb_pcmcia_exit(struct ssb_bus *bus) |
| 805 | { |
| 806 | if (bus->bustype != SSB_BUSTYPE_PCMCIA) |
| 807 | return; |
| 808 | |
| 809 | device_remove_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom); |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | int ssb_pcmcia_init(struct ssb_bus *bus) |
| 813 | { |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 814 | int err; |
| 815 | |
| 816 | if (bus->bustype != SSB_BUSTYPE_PCMCIA) |
| 817 | return 0; |
| 818 | |
Michael Buesch | 8fe2b65 | 2008-03-30 00:10:50 +0100 | [diff] [blame] | 819 | err = ssb_pcmcia_hardware_setup(bus); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 820 | if (err) |
| 821 | goto error; |
| 822 | |
| 823 | bus->sprom_size = SSB_PCMCIA_SPROM_SIZE; |
| 824 | mutex_init(&bus->sprom_mutex); |
| 825 | err = device_create_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom); |
| 826 | if (err) |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 827 | goto error; |
| 828 | |
| 829 | return 0; |
| 830 | error: |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 831 | pr_err("Failed to initialize PCMCIA host device\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 832 | return err; |
Michael Buesch | 61e115a | 2007-09-18 15:12:50 -0400 | [diff] [blame] | 833 | } |