sprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "ssb_private.h"
  14. static const struct ssb_sprom *fallback_sprom;
  15. static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
  16. size_t sprom_size_words)
  17. {
  18. int i, pos = 0;
  19. for (i = 0; i < sprom_size_words; i++)
  20. pos += snprintf(buf + pos, buf_len - pos - 1,
  21. "%04X", swab16(sprom[i]) & 0xFFFF);
  22. pos += snprintf(buf + pos, buf_len - pos - 1, "\n");
  23. return pos + 1;
  24. }
  25. static int hex2sprom(u16 *sprom, const char *dump, size_t len,
  26. size_t sprom_size_words)
  27. {
  28. char tmp[5] = { 0 };
  29. int cnt = 0;
  30. unsigned long parsed;
  31. if (len < sprom_size_words * 2)
  32. return -EINVAL;
  33. while (cnt < sprom_size_words) {
  34. memcpy(tmp, dump, 4);
  35. dump += 4;
  36. parsed = simple_strtoul(tmp, NULL, 16);
  37. sprom[cnt++] = swab16((u16)parsed);
  38. }
  39. return 0;
  40. }
  41. /* Common sprom device-attribute show-handler */
  42. ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf,
  43. int (*sprom_read)(struct ssb_bus *bus, u16 *sprom))
  44. {
  45. u16 *sprom;
  46. int err = -ENOMEM;
  47. ssize_t count = 0;
  48. size_t sprom_size_words = bus->sprom_size;
  49. sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
  50. if (!sprom)
  51. goto out;
  52. /* Use interruptible locking, as the SPROM write might
  53. * be holding the lock for several seconds. So allow userspace
  54. * to cancel operation. */
  55. err = -ERESTARTSYS;
  56. if (mutex_lock_interruptible(&bus->sprom_mutex))
  57. goto out_kfree;
  58. err = sprom_read(bus, sprom);
  59. mutex_unlock(&bus->sprom_mutex);
  60. if (!err)
  61. count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
  62. out_kfree:
  63. kfree(sprom);
  64. out:
  65. return err ? err : count;
  66. }
  67. /* Common sprom device-attribute store-handler */
  68. ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
  69. const char *buf, size_t count,
  70. int (*sprom_check_crc)(const u16 *sprom, size_t size),
  71. int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
  72. {
  73. u16 *sprom;
  74. int res = 0, err = -ENOMEM;
  75. size_t sprom_size_words = bus->sprom_size;
  76. struct ssb_freeze_context freeze;
  77. sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
  78. if (!sprom)
  79. goto out;
  80. err = hex2sprom(sprom, buf, count, sprom_size_words);
  81. if (err) {
  82. err = -EINVAL;
  83. goto out_kfree;
  84. }
  85. err = sprom_check_crc(sprom, sprom_size_words);
  86. if (err) {
  87. err = -EINVAL;
  88. goto out_kfree;
  89. }
  90. /* Use interruptible locking, as the SPROM write might
  91. * be holding the lock for several seconds. So allow userspace
  92. * to cancel operation. */
  93. err = -ERESTARTSYS;
  94. if (mutex_lock_interruptible(&bus->sprom_mutex))
  95. goto out_kfree;
  96. err = ssb_devices_freeze(bus, &freeze);
  97. if (err) {
  98. ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze all devices\n");
  99. goto out_unlock;
  100. }
  101. res = sprom_write(bus, sprom);
  102. err = ssb_devices_thaw(&freeze);
  103. if (err)
  104. ssb_printk(KERN_ERR PFX "SPROM write: Could not thaw all devices\n");
  105. out_unlock:
  106. mutex_unlock(&bus->sprom_mutex);
  107. out_kfree:
  108. kfree(sprom);
  109. out:
  110. if (res)
  111. return res;
  112. return err ? err : count;
  113. }
  114. /**
  115. * ssb_arch_set_fallback_sprom - Set a fallback SPROM for use if no SPROM is found.
  116. *
  117. * @sprom: The SPROM data structure to register.
  118. *
  119. * With this function the architecture implementation may register a fallback
  120. * SPROM data structure. The fallback is only used for PCI based SSB devices,
  121. * where no valid SPROM can be found in the shadow registers.
  122. *
  123. * This function is useful for weird architectures that have a half-assed SSB device
  124. * hardwired to their PCI bus.
  125. *
  126. * Note that it does only work with PCI attached SSB devices. PCMCIA devices currently
  127. * don't use this fallback.
  128. * Architectures must provide the SPROM for native SSB devices anyway,
  129. * so the fallback also isn't used for native devices.
  130. *
  131. * This function is available for architecture code, only. So it is not exported.
  132. */
  133. int ssb_arch_set_fallback_sprom(const struct ssb_sprom *sprom)
  134. {
  135. if (fallback_sprom)
  136. return -EEXIST;
  137. fallback_sprom = sprom;
  138. return 0;
  139. }
  140. const struct ssb_sprom *ssb_get_fallback_sprom(void)
  141. {
  142. return fallback_sprom;
  143. }