2 * Sonics Silicon Backplane
3 * SDIO-Hostbus related functions
5 * Copyright 2009 Albert Herranz <albert_herranz@yahoo.es>
7 * Based on drivers/ssb/pcmcia.c
8 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
9 * Copyright 2007-2008 Michael Buesch <m@bues.ch>
11 * Licensed under the GNU/GPL. See COPYING for details.
15 #include <linux/ssb/ssb.h>
16 #include <linux/delay.h>
18 #include <linux/etherdevice.h>
19 #include <linux/mmc/sdio_func.h>
21 #include "ssb_private.h"
23 /* Define the following to 1 to enable a printk on each coreswitch. */
24 #define SSB_VERBOSE_SDIOCORESWITCH_DEBUG 0
27 /* Hardware invariants CIS tuples */
28 #define SSB_SDIO_CIS 0x80
29 #define SSB_SDIO_CIS_SROMREV 0x00
30 #define SSB_SDIO_CIS_ID 0x01
31 #define SSB_SDIO_CIS_BOARDREV 0x02
32 #define SSB_SDIO_CIS_PA 0x03
33 #define SSB_SDIO_CIS_PA_PA0B0_LO 0
34 #define SSB_SDIO_CIS_PA_PA0B0_HI 1
35 #define SSB_SDIO_CIS_PA_PA0B1_LO 2
36 #define SSB_SDIO_CIS_PA_PA0B1_HI 3
37 #define SSB_SDIO_CIS_PA_PA0B2_LO 4
38 #define SSB_SDIO_CIS_PA_PA0B2_HI 5
39 #define SSB_SDIO_CIS_PA_ITSSI 6
40 #define SSB_SDIO_CIS_PA_MAXPOW 7
41 #define SSB_SDIO_CIS_OEMNAME 0x04
42 #define SSB_SDIO_CIS_CCODE 0x05
43 #define SSB_SDIO_CIS_ANTENNA 0x06
44 #define SSB_SDIO_CIS_ANTGAIN 0x07
45 #define SSB_SDIO_CIS_BFLAGS 0x08
46 #define SSB_SDIO_CIS_LEDS 0x09
48 #define CISTPL_FUNCE_LAN_NODE_ID 0x04 /* same as in PCMCIA */
52 * Function 1 miscellaneous registers.
54 * Definitions match src/include/sbsdio.h from the
55 * Android Open Source Project
56 * http://android.git.kernel.org/?p=platform/system/wlan/broadcom.git
59 #define SBSDIO_FUNC1_SBADDRLOW 0x1000a /* SB Address window Low (b15) */
60 #define SBSDIO_FUNC1_SBADDRMID 0x1000b /* SB Address window Mid (b23-b16) */
61 #define SBSDIO_FUNC1_SBADDRHIGH 0x1000c /* SB Address window High (b24-b31) */
63 /* valid bits in SBSDIO_FUNC1_SBADDRxxx regs */
64 #define SBSDIO_SBADDRLOW_MASK 0x80 /* Valid address bits in SBADDRLOW */
65 #define SBSDIO_SBADDRMID_MASK 0xff /* Valid address bits in SBADDRMID */
66 #define SBSDIO_SBADDRHIGH_MASK 0xff /* Valid address bits in SBADDRHIGH */
68 #define SBSDIO_SB_OFT_ADDR_MASK 0x7FFF /* sb offset addr is <= 15 bits, 32k */
70 /* REVISIT: this flag doesn't seem to matter */
71 #define SBSDIO_SB_ACCESS_2_4B_FLAG 0x8000 /* forces 32-bit SB access */
75 * Address map within the SDIO function address space (128K).
77 * Start End Description
78 * ------- ------- ------------------------------------------
79 * 0x00000 0x0ffff selected backplane address window (64K)
80 * 0x10000 0x1ffff backplane control registers (max 64K)
82 * The current address window is configured by writing to registers
83 * SBADDRLOW, SBADDRMID and SBADDRHIGH.
85 * In order to access the contents of a 32-bit Silicon Backplane address
86 * the backplane address window must be first loaded with the highest
87 * 16 bits of the target address. Then, an access must be done to the
88 * SDIO function address space using the lower 15 bits of the address.
89 * Bit 15 of the address must be set when doing 32 bit accesses.
91 * 10987654321098765432109876543210
92 * WWWWWWWWWWWWWWWWW SB Address Window
93 * OOOOOOOOOOOOOOOO Offset within SB Address Window
94 * a 32-bit access flag
101 * NOTE: SDIO address @addr is 17 bits long (SDIO address space is 128K).
104 static inline struct device *ssb_sdio_dev(struct ssb_bus *bus)
106 return &bus->host_sdio->dev;
110 static int ssb_sdio_writeb(struct ssb_bus *bus, unsigned int addr, u8 val)
114 sdio_writeb(bus->host_sdio, val, addr, &error);
115 if (unlikely(error)) {
116 dev_dbg(ssb_sdio_dev(bus), "%08X <- %02x, error %d\n",
124 static u8 ssb_sdio_readb(struct ssb_bus *bus, unsigned int addr)
129 val = sdio_readb(bus->host_sdio, addr, &error);
130 if (unlikely(error)) {
131 dev_dbg(ssb_sdio_dev(bus), "%08X -> %02x, error %d\n",
140 static int ssb_sdio_set_sbaddr_window(struct ssb_bus *bus, u32 address)
144 error = ssb_sdio_writeb(bus, SBSDIO_FUNC1_SBADDRLOW,
145 (address >> 8) & SBSDIO_SBADDRLOW_MASK);
148 error = ssb_sdio_writeb(bus, SBSDIO_FUNC1_SBADDRMID,
149 (address >> 16) & SBSDIO_SBADDRMID_MASK);
152 error = ssb_sdio_writeb(bus, SBSDIO_FUNC1_SBADDRHIGH,
153 (address >> 24) & SBSDIO_SBADDRHIGH_MASK);
156 bus->sdio_sbaddr = address;
159 dev_dbg(ssb_sdio_dev(bus), "failed to set address window"
160 " to 0x%08x, error %d\n", address, error);
166 /* for enumeration use only */
167 u32 ssb_sdio_scan_read32(struct ssb_bus *bus, u16 offset)
172 sdio_claim_host(bus->host_sdio);
173 val = sdio_readl(bus->host_sdio, offset, &error);
174 sdio_release_host(bus->host_sdio);
175 if (unlikely(error)) {
176 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %08x, error %d\n",
177 bus->sdio_sbaddr >> 16, offset, val, error);
183 /* for enumeration use only */
184 int ssb_sdio_scan_switch_coreidx(struct ssb_bus *bus, u8 coreidx)
189 sbaddr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
190 sdio_claim_host(bus->host_sdio);
191 error = ssb_sdio_set_sbaddr_window(bus, sbaddr);
192 sdio_release_host(bus->host_sdio);
194 dev_err(ssb_sdio_dev(bus), "failed to switch to core %u,"
195 " error %d\n", coreidx, error);
202 /* host must be already claimed */
203 static int ssb_sdio_switch_core(struct ssb_bus *bus, struct ssb_device *dev)
205 u8 coreidx = dev->core_index;
209 sbaddr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
210 if (unlikely(bus->sdio_sbaddr != sbaddr)) {
211 #if SSB_VERBOSE_SDIOCORESWITCH_DEBUG
212 dev_info(ssb_sdio_dev(bus),
213 "switching to %s core, index %d\n",
214 ssb_core_name(dev->id.coreid), coreidx);
216 error = ssb_sdio_set_sbaddr_window(bus, sbaddr);
218 dev_dbg(ssb_sdio_dev(bus), "failed to switch to"
219 " core %u, error %d\n", coreidx, error);
222 bus->mapped_device = dev;
229 static u8 ssb_sdio_read8(struct ssb_device *dev, u16 offset)
231 struct ssb_bus *bus = dev->bus;
235 sdio_claim_host(bus->host_sdio);
236 if (unlikely(ssb_sdio_switch_core(bus, dev)))
238 offset |= bus->sdio_sbaddr & 0xffff;
239 offset &= SBSDIO_SB_OFT_ADDR_MASK;
240 val = sdio_readb(bus->host_sdio, offset, &error);
242 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %02x, error %d\n",
243 bus->sdio_sbaddr >> 16, offset, val, error);
246 sdio_release_host(bus->host_sdio);
251 static u16 ssb_sdio_read16(struct ssb_device *dev, u16 offset)
253 struct ssb_bus *bus = dev->bus;
257 sdio_claim_host(bus->host_sdio);
258 if (unlikely(ssb_sdio_switch_core(bus, dev)))
260 offset |= bus->sdio_sbaddr & 0xffff;
261 offset &= SBSDIO_SB_OFT_ADDR_MASK;
262 val = sdio_readw(bus->host_sdio, offset, &error);
264 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %04x, error %d\n",
265 bus->sdio_sbaddr >> 16, offset, val, error);
268 sdio_release_host(bus->host_sdio);
273 static u32 ssb_sdio_read32(struct ssb_device *dev, u16 offset)
275 struct ssb_bus *bus = dev->bus;
276 u32 val = 0xffffffff;
279 sdio_claim_host(bus->host_sdio);
280 if (unlikely(ssb_sdio_switch_core(bus, dev)))
282 offset |= bus->sdio_sbaddr & 0xffff;
283 offset &= SBSDIO_SB_OFT_ADDR_MASK;
284 offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
285 val = sdio_readl(bus->host_sdio, offset, &error);
287 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X > %08x, error %d\n",
288 bus->sdio_sbaddr >> 16, offset, val, error);
291 sdio_release_host(bus->host_sdio);
296 #ifdef CONFIG_SSB_BLOCKIO
297 static void ssb_sdio_block_read(struct ssb_device *dev, void *buffer,
298 size_t count, u16 offset, u8 reg_width)
300 size_t saved_count = count;
301 struct ssb_bus *bus = dev->bus;
304 sdio_claim_host(bus->host_sdio);
305 if (unlikely(ssb_sdio_switch_core(bus, dev))) {
307 memset(buffer, 0xff, count);
310 offset |= bus->sdio_sbaddr & 0xffff;
311 offset &= SBSDIO_SB_OFT_ADDR_MASK;
315 error = sdio_readsb(bus->host_sdio, buffer, offset, count);
319 SSB_WARN_ON(count & 1);
320 error = sdio_readsb(bus->host_sdio, buffer, offset, count);
324 SSB_WARN_ON(count & 3);
325 offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
326 error = sdio_readsb(bus->host_sdio, buffer, offset, count);
336 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X (width=%u, len=%zu), error %d\n",
337 bus->sdio_sbaddr >> 16, offset, reg_width, saved_count, error);
339 sdio_release_host(bus->host_sdio);
341 #endif /* CONFIG_SSB_BLOCKIO */
343 static void ssb_sdio_write8(struct ssb_device *dev, u16 offset, u8 val)
345 struct ssb_bus *bus = dev->bus;
348 sdio_claim_host(bus->host_sdio);
349 if (unlikely(ssb_sdio_switch_core(bus, dev)))
351 offset |= bus->sdio_sbaddr & 0xffff;
352 offset &= SBSDIO_SB_OFT_ADDR_MASK;
353 sdio_writeb(bus->host_sdio, val, offset, &error);
355 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X < %02x, error %d\n",
356 bus->sdio_sbaddr >> 16, offset, val, error);
359 sdio_release_host(bus->host_sdio);
362 static void ssb_sdio_write16(struct ssb_device *dev, u16 offset, u16 val)
364 struct ssb_bus *bus = dev->bus;
367 sdio_claim_host(bus->host_sdio);
368 if (unlikely(ssb_sdio_switch_core(bus, dev)))
370 offset |= bus->sdio_sbaddr & 0xffff;
371 offset &= SBSDIO_SB_OFT_ADDR_MASK;
372 sdio_writew(bus->host_sdio, val, offset, &error);
374 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X < %04x, error %d\n",
375 bus->sdio_sbaddr >> 16, offset, val, error);
378 sdio_release_host(bus->host_sdio);
381 static void ssb_sdio_write32(struct ssb_device *dev, u16 offset, u32 val)
383 struct ssb_bus *bus = dev->bus;
386 sdio_claim_host(bus->host_sdio);
387 if (unlikely(ssb_sdio_switch_core(bus, dev)))
389 offset |= bus->sdio_sbaddr & 0xffff;
390 offset &= SBSDIO_SB_OFT_ADDR_MASK;
391 offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
392 sdio_writel(bus->host_sdio, val, offset, &error);
394 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X < %08x, error %d\n",
395 bus->sdio_sbaddr >> 16, offset, val, error);
397 if (bus->quirks & SSB_QUIRK_SDIO_READ_AFTER_WRITE32)
398 sdio_readl(bus->host_sdio, 0, &error);
400 sdio_release_host(bus->host_sdio);
403 #ifdef CONFIG_SSB_BLOCKIO
404 static void ssb_sdio_block_write(struct ssb_device *dev, const void *buffer,
405 size_t count, u16 offset, u8 reg_width)
407 size_t saved_count = count;
408 struct ssb_bus *bus = dev->bus;
411 sdio_claim_host(bus->host_sdio);
412 if (unlikely(ssb_sdio_switch_core(bus, dev))) {
414 memset((void *)buffer, 0xff, count);
417 offset |= bus->sdio_sbaddr & 0xffff;
418 offset &= SBSDIO_SB_OFT_ADDR_MASK;
422 error = sdio_writesb(bus->host_sdio, offset,
423 (void *)buffer, count);
426 SSB_WARN_ON(count & 1);
427 error = sdio_writesb(bus->host_sdio, offset,
428 (void *)buffer, count);
431 SSB_WARN_ON(count & 3);
432 offset |= SBSDIO_SB_ACCESS_2_4B_FLAG; /* 32 bit data access */
433 error = sdio_writesb(bus->host_sdio, offset,
434 (void *)buffer, count);
443 dev_dbg(ssb_sdio_dev(bus), "%04X:%04X (width=%u, len=%zu), error %d\n",
444 bus->sdio_sbaddr >> 16, offset, reg_width, saved_count, error);
446 sdio_release_host(bus->host_sdio);
449 #endif /* CONFIG_SSB_BLOCKIO */
451 /* Not "static", as it's used in main.c */
452 const struct ssb_bus_ops ssb_sdio_ops = {
453 .read8 = ssb_sdio_read8,
454 .read16 = ssb_sdio_read16,
455 .read32 = ssb_sdio_read32,
456 .write8 = ssb_sdio_write8,
457 .write16 = ssb_sdio_write16,
458 .write32 = ssb_sdio_write32,
459 #ifdef CONFIG_SSB_BLOCKIO
460 .block_read = ssb_sdio_block_read,
461 .block_write = ssb_sdio_block_write,
465 #define GOTO_ERROR_ON(condition, description) do { \
466 if (unlikely(condition)) { \
467 error_description = description; \
472 int ssb_sdio_get_invariants(struct ssb_bus *bus,
473 struct ssb_init_invariants *iv)
475 struct ssb_sprom *sprom = &iv->sprom;
476 struct ssb_boardinfo *bi = &iv->boardinfo;
477 const char *error_description = "none";
478 struct sdio_func_tuple *tuple;
481 memset(sprom, 0xFF, sizeof(*sprom));
482 sprom->boardflags_lo = 0;
483 sprom->boardflags_hi = 0;
485 tuple = bus->host_sdio->tuples;
487 switch (tuple->code) {
488 case 0x22: /* extended function */
489 switch (tuple->data[0]) {
490 case CISTPL_FUNCE_LAN_NODE_ID:
491 GOTO_ERROR_ON((tuple->size != 7) &&
492 (tuple->data[1] != 6),
494 /* fetch the MAC address. */
495 mac = tuple->data + 2;
496 memcpy(sprom->il0mac, mac, ETH_ALEN);
497 memcpy(sprom->et1mac, mac, ETH_ALEN);
503 case 0x80: /* vendor specific tuple */
504 switch (tuple->data[0]) {
505 case SSB_SDIO_CIS_SROMREV:
506 GOTO_ERROR_ON(tuple->size != 2,
508 sprom->revision = tuple->data[1];
510 case SSB_SDIO_CIS_ID:
511 GOTO_ERROR_ON((tuple->size != 5) &&
514 bi->vendor = tuple->data[1] |
517 case SSB_SDIO_CIS_BOARDREV:
518 GOTO_ERROR_ON(tuple->size != 2,
519 "boardrev tpl size");
520 sprom->board_rev = tuple->data[1];
522 case SSB_SDIO_CIS_PA:
523 GOTO_ERROR_ON((tuple->size != 9) &&
526 sprom->pa0b0 = tuple->data[1] |
527 ((u16)tuple->data[2] << 8);
528 sprom->pa0b1 = tuple->data[3] |
529 ((u16)tuple->data[4] << 8);
530 sprom->pa0b2 = tuple->data[5] |
531 ((u16)tuple->data[6] << 8);
532 sprom->itssi_a = tuple->data[7];
533 sprom->itssi_bg = tuple->data[7];
534 sprom->maxpwr_a = tuple->data[8];
535 sprom->maxpwr_bg = tuple->data[8];
537 case SSB_SDIO_CIS_OEMNAME:
540 case SSB_SDIO_CIS_CCODE:
541 GOTO_ERROR_ON(tuple->size != 2,
543 sprom->country_code = tuple->data[1];
545 case SSB_SDIO_CIS_ANTENNA:
546 GOTO_ERROR_ON(tuple->size != 2,
548 sprom->ant_available_a = tuple->data[1];
549 sprom->ant_available_bg = tuple->data[1];
551 case SSB_SDIO_CIS_ANTGAIN:
552 GOTO_ERROR_ON(tuple->size != 2,
554 sprom->antenna_gain.a0 = tuple->data[1];
555 sprom->antenna_gain.a1 = tuple->data[1];
556 sprom->antenna_gain.a2 = tuple->data[1];
557 sprom->antenna_gain.a3 = tuple->data[1];
559 case SSB_SDIO_CIS_BFLAGS:
560 GOTO_ERROR_ON((tuple->size != 3) &&
563 sprom->boardflags_lo = tuple->data[1] |
564 ((u16)tuple->data[2] << 8);
566 case SSB_SDIO_CIS_LEDS:
567 GOTO_ERROR_ON(tuple->size != 5,
569 sprom->gpio0 = tuple->data[1];
570 sprom->gpio1 = tuple->data[2];
571 sprom->gpio2 = tuple->data[3];
572 sprom->gpio3 = tuple->data[4];
586 dev_err(ssb_sdio_dev(bus), "failed to fetch device invariants: %s\n",
591 void ssb_sdio_exit(struct ssb_bus *bus)
593 if (bus->bustype != SSB_BUSTYPE_SDIO)
595 /* Nothing to do here. */
598 int ssb_sdio_init(struct ssb_bus *bus)
600 if (bus->bustype != SSB_BUSTYPE_SDIO)
603 bus->sdio_sbaddr = ~0;