opal-xscom.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * PowerNV LPC bus handling.
  3. *
  4. * Copyright 2013 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/bug.h>
  14. #include <linux/gfp.h>
  15. #include <linux/slab.h>
  16. #include <asm/machdep.h>
  17. #include <asm/firmware.h>
  18. #include <asm/opal.h>
  19. #include <asm/scom.h>
  20. /*
  21. * We could probably fit that inside the scom_map_t
  22. * which is a void* after all but it's really too ugly
  23. * so let's kmalloc it for now
  24. */
  25. struct opal_scom_map {
  26. uint32_t chip;
  27. uint64_t addr;
  28. };
  29. static scom_map_t opal_scom_map(struct device_node *dev, u64 reg, u64 count)
  30. {
  31. struct opal_scom_map *m;
  32. const __be32 *gcid;
  33. if (!of_get_property(dev, "scom-controller", NULL)) {
  34. pr_err("%s: device %s is not a SCOM controller\n",
  35. __func__, dev->full_name);
  36. return SCOM_MAP_INVALID;
  37. }
  38. gcid = of_get_property(dev, "ibm,chip-id", NULL);
  39. if (!gcid) {
  40. pr_err("%s: device %s has no ibm,chip-id\n",
  41. __func__, dev->full_name);
  42. return SCOM_MAP_INVALID;
  43. }
  44. m = kmalloc(sizeof(struct opal_scom_map), GFP_KERNEL);
  45. if (!m)
  46. return NULL;
  47. m->chip = be32_to_cpup(gcid);
  48. m->addr = reg;
  49. return (scom_map_t)m;
  50. }
  51. static void opal_scom_unmap(scom_map_t map)
  52. {
  53. kfree(map);
  54. }
  55. static int opal_xscom_err_xlate(int64_t rc)
  56. {
  57. switch(rc) {
  58. case 0:
  59. return 0;
  60. /* Add more translations if necessary */
  61. default:
  62. return -EIO;
  63. }
  64. }
  65. static u64 opal_scom_unmangle(u64 addr)
  66. {
  67. /*
  68. * XSCOM indirect addresses have the top bit set. Additionally
  69. * the rest of the top 3 nibbles is always 0.
  70. *
  71. * Because the debugfs interface uses signed offsets and shifts
  72. * the address left by 3, we basically cannot use the top 4 bits
  73. * of the 64-bit address, and thus cannot use the indirect bit.
  74. *
  75. * To deal with that, we support the indirect bit being in bit
  76. * 4 (IBM notation) instead of bit 0 in this API, we do the
  77. * conversion here. To leave room for further xscom address
  78. * expansion, we only clear out the top byte
  79. *
  80. * For in-kernel use, we also support the real indirect bit, so
  81. * we test for any of the top 5 bits
  82. *
  83. */
  84. if (addr & (0x1full << 59))
  85. addr = (addr & ~(0xffull << 56)) | (1ull << 63);
  86. return addr;
  87. }
  88. static int opal_scom_read(scom_map_t map, u64 reg, u64 *value)
  89. {
  90. struct opal_scom_map *m = map;
  91. int64_t rc;
  92. __be64 v;
  93. reg = opal_scom_unmangle(m->addr + reg);
  94. rc = opal_xscom_read(m->chip, reg, (__be64 *)__pa(&v));
  95. *value = be64_to_cpu(v);
  96. return opal_xscom_err_xlate(rc);
  97. }
  98. static int opal_scom_write(scom_map_t map, u64 reg, u64 value)
  99. {
  100. struct opal_scom_map *m = map;
  101. int64_t rc;
  102. reg = opal_scom_unmangle(m->addr + reg);
  103. rc = opal_xscom_write(m->chip, reg, value);
  104. return opal_xscom_err_xlate(rc);
  105. }
  106. static const struct scom_controller opal_scom_controller = {
  107. .map = opal_scom_map,
  108. .unmap = opal_scom_unmap,
  109. .read = opal_scom_read,
  110. .write = opal_scom_write
  111. };
  112. static int opal_xscom_init(void)
  113. {
  114. if (firmware_has_feature(FW_FEATURE_OPAL))
  115. scom_init(&opal_scom_controller);
  116. return 0;
  117. }
  118. machine_arch_initcall(powernv, opal_xscom_init);