Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Sonics Silicon Backplane |
| 3 | * Common SPROM support routines |
| 4 | * |
| 5 | * Copyright (C) 2005-2008 Michael Buesch <mb@bu3sch.de> |
| 6 | * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de> |
| 7 | * Copyright (C) 2005 Stefano Brivio <st3@riseup.net> |
| 8 | * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org> |
| 9 | * Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch> |
| 10 | * |
| 11 | * Licensed under the GNU/GPL. See COPYING for details. |
| 12 | */ |
| 13 | |
| 14 | #include "ssb_private.h" |
| 15 | |
| 16 | |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 17 | static const struct ssb_sprom *fallback_sprom; |
| 18 | |
| 19 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 20 | static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len, |
| 21 | size_t sprom_size_words) |
| 22 | { |
| 23 | int i, pos = 0; |
| 24 | |
| 25 | for (i = 0; i < sprom_size_words; i++) |
| 26 | pos += snprintf(buf + pos, buf_len - pos - 1, |
| 27 | "%04X", swab16(sprom[i]) & 0xFFFF); |
| 28 | pos += snprintf(buf + pos, buf_len - pos - 1, "\n"); |
| 29 | |
| 30 | return pos + 1; |
| 31 | } |
| 32 | |
| 33 | static int hex2sprom(u16 *sprom, const char *dump, size_t len, |
| 34 | size_t sprom_size_words) |
| 35 | { |
| 36 | char tmp[5] = { 0 }; |
| 37 | int cnt = 0; |
| 38 | unsigned long parsed; |
| 39 | |
| 40 | if (len < sprom_size_words * 2) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | while (cnt < sprom_size_words) { |
| 44 | memcpy(tmp, dump, 4); |
| 45 | dump += 4; |
| 46 | parsed = simple_strtoul(tmp, NULL, 16); |
| 47 | sprom[cnt++] = swab16((u16)parsed); |
| 48 | } |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /* Common sprom device-attribute show-handler */ |
| 54 | ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf, |
| 55 | int (*sprom_read)(struct ssb_bus *bus, u16 *sprom)) |
| 56 | { |
| 57 | u16 *sprom; |
| 58 | int err = -ENOMEM; |
| 59 | ssize_t count = 0; |
| 60 | size_t sprom_size_words = bus->sprom_size; |
| 61 | |
| 62 | sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL); |
| 63 | if (!sprom) |
| 64 | goto out; |
| 65 | |
| 66 | /* Use interruptible locking, as the SPROM write might |
| 67 | * be holding the lock for several seconds. So allow userspace |
| 68 | * to cancel operation. */ |
| 69 | err = -ERESTARTSYS; |
| 70 | if (mutex_lock_interruptible(&bus->sprom_mutex)) |
| 71 | goto out_kfree; |
| 72 | err = sprom_read(bus, sprom); |
| 73 | mutex_unlock(&bus->sprom_mutex); |
| 74 | |
| 75 | if (!err) |
| 76 | count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words); |
| 77 | |
| 78 | out_kfree: |
| 79 | kfree(sprom); |
| 80 | out: |
| 81 | return err ? err : count; |
| 82 | } |
| 83 | |
| 84 | /* Common sprom device-attribute store-handler */ |
| 85 | ssize_t ssb_attr_sprom_store(struct ssb_bus *bus, |
| 86 | const char *buf, size_t count, |
| 87 | int (*sprom_check_crc)(const u16 *sprom, size_t size), |
| 88 | int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom)) |
| 89 | { |
| 90 | u16 *sprom; |
| 91 | int res = 0, err = -ENOMEM; |
| 92 | size_t sprom_size_words = bus->sprom_size; |
| 93 | |
| 94 | sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL); |
| 95 | if (!sprom) |
| 96 | goto out; |
| 97 | err = hex2sprom(sprom, buf, count, sprom_size_words); |
| 98 | if (err) { |
| 99 | err = -EINVAL; |
| 100 | goto out_kfree; |
| 101 | } |
| 102 | err = sprom_check_crc(sprom, sprom_size_words); |
| 103 | if (err) { |
| 104 | err = -EINVAL; |
| 105 | goto out_kfree; |
| 106 | } |
| 107 | |
| 108 | /* Use interruptible locking, as the SPROM write might |
| 109 | * be holding the lock for several seconds. So allow userspace |
| 110 | * to cancel operation. */ |
| 111 | err = -ERESTARTSYS; |
| 112 | if (mutex_lock_interruptible(&bus->sprom_mutex)) |
| 113 | goto out_kfree; |
| 114 | err = ssb_devices_freeze(bus); |
| 115 | if (err == -EOPNOTSUPP) { |
| 116 | ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze devices. " |
| 117 | "No suspend support. Is CONFIG_PM enabled?\n"); |
| 118 | goto out_unlock; |
| 119 | } |
| 120 | if (err) { |
| 121 | ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze all devices\n"); |
| 122 | goto out_unlock; |
| 123 | } |
| 124 | res = sprom_write(bus, sprom); |
| 125 | err = ssb_devices_thaw(bus); |
| 126 | if (err) |
| 127 | ssb_printk(KERN_ERR PFX "SPROM write: Could not thaw all devices\n"); |
| 128 | out_unlock: |
| 129 | mutex_unlock(&bus->sprom_mutex); |
| 130 | out_kfree: |
| 131 | kfree(sprom); |
| 132 | out: |
| 133 | if (res) |
| 134 | return res; |
| 135 | return err ? err : count; |
| 136 | } |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * ssb_arch_set_fallback_sprom - Set a fallback SPROM for use if no SPROM is found. |
| 140 | * |
| 141 | * @sprom: The SPROM data structure to register. |
| 142 | * |
| 143 | * With this function the architecture implementation may register a fallback |
| 144 | * SPROM data structure. The fallback is only used for PCI based SSB devices, |
| 145 | * where no valid SPROM can be found in the shadow registers. |
| 146 | * |
| 147 | * This function is useful for weird architectures that have a half-assed SSB device |
| 148 | * hardwired to their PCI bus. |
| 149 | * |
| 150 | * Note that it does only work with PCI attached SSB devices. PCMCIA devices currently |
| 151 | * don't use this fallback. |
| 152 | * Architectures must provide the SPROM for native SSB devices anyway, |
| 153 | * so the fallback also isn't used for native devices. |
| 154 | * |
| 155 | * This function is available for architecture code, only. So it is not exported. |
| 156 | */ |
| 157 | int ssb_arch_set_fallback_sprom(const struct ssb_sprom *sprom) |
| 158 | { |
| 159 | if (fallback_sprom) |
| 160 | return -EEXIST; |
| 161 | fallback_sprom = sprom; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | const struct ssb_sprom *ssb_get_fallback_sprom(void) |
| 167 | { |
| 168 | return fallback_sprom; |
| 169 | } |