^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Sonics Silicon Backplane
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Common SPROM support routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005-2008 Michael Buesch <m@bues.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Licensed under the GNU/GPL. See COPYING for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "ssb_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int(*get_fallback_sprom)(struct ssb_bus *dev, struct ssb_sprom *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) size_t sprom_size_words)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int i, pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) for (i = 0; i < sprom_size_words; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) pos += scnprintf(buf + pos, buf_len - pos - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) "%04X", swab16(sprom[i]) & 0xFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) pos += scnprintf(buf + pos, buf_len - pos - 1, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return pos + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int hex2sprom(u16 *sprom, const char *dump, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) size_t sprom_size_words)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) char c, tmp[5] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int err, cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long parsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Strip whitespace at the end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) c = dump[len - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!isspace(c) && c != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Length must match exactly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (len != sprom_size_words * 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) while (cnt < sprom_size_words) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memcpy(tmp, dump, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dump += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) err = kstrtoul(tmp, 16, &parsed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) sprom[cnt++] = swab16((u16)parsed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* Common sprom device-attribute show-handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int (*sprom_read)(struct ssb_bus *bus, u16 *sprom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) u16 *sprom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) ssize_t count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) size_t sprom_size_words = bus->sprom_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!sprom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Use interruptible locking, as the SPROM write might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * be holding the lock for several seconds. So allow userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * to cancel operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) err = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (mutex_lock_interruptible(&bus->sprom_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) err = sprom_read(bus, sprom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_unlock(&bus->sprom_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) out_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kfree(sprom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return err ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Common sprom device-attribute store-handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) const char *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int (*sprom_check_crc)(const u16 *sprom, size_t size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u16 *sprom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int res = 0, err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) size_t sprom_size_words = bus->sprom_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct ssb_freeze_context freeze;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!sprom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err = hex2sprom(sprom, buf, count, sprom_size_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) err = sprom_check_crc(sprom, sprom_size_words);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Use interruptible locking, as the SPROM write might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * be holding the lock for several seconds. So allow userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * to cancel operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) err = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (mutex_lock_interruptible(&bus->sprom_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) err = ssb_devices_freeze(bus, &freeze);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_err("SPROM write: Could not freeze all devices\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) res = sprom_write(bus, sprom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) err = ssb_devices_thaw(&freeze);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pr_err("SPROM write: Could not thaw all devices\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) mutex_unlock(&bus->sprom_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) out_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) kfree(sprom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return err ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * ssb_arch_register_fallback_sprom - Registers a method providing a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * fallback SPROM if no SPROM is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @sprom_callback: The callback function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * With this function the architecture implementation may register a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * callback handler which fills the SPROM data structure. The fallback is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * only used for PCI based SSB devices, where no valid SPROM can be found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * in the shadow registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * This function is useful for weird architectures that have a half-assed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * SSB device hardwired to their PCI bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Note that it does only work with PCI attached SSB devices. PCMCIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * devices currently don't use this fallback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * Architectures must provide the SPROM for native SSB devices anyway, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * the fallback also isn't used for native devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * This function is available for architecture code, only. So it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * exported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int ssb_arch_register_fallback_sprom(int (*sprom_callback)(struct ssb_bus *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct ssb_sprom *out))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (get_fallback_sprom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) get_fallback_sprom = sprom_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ssb_fill_sprom_with_fallback(struct ssb_bus *bus, struct ssb_sprom *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!get_fallback_sprom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return get_fallback_sprom(bus, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* https://bcm-v4.sipsolutions.net/802.11/IsSpromAvailable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) bool ssb_is_sprom_available(struct ssb_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* status register only exists on chipcomon rev >= 11 and we need check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * for >= 31 only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* this routine differs from specs as we do not access SPROM directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * on PCMCIA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (bus->bustype == SSB_BUSTYPE_PCI &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) bus->chipco.dev && /* can be unavailable! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) bus->chipco.dev->id.revision >= 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return bus->chipco.capabilities & SSB_CHIPCO_CAP_SPROM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }