opal-xscom.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 int opal_scom_read(scom_map_t map, u64 reg, u64 *value)
  66. {
  67. struct opal_scom_map *m = map;
  68. int64_t rc;
  69. rc = opal_xscom_read(m->chip, m->addr + reg, (uint64_t *)__pa(value));
  70. return opal_xscom_err_xlate(rc);
  71. }
  72. static int opal_scom_write(scom_map_t map, u64 reg, u64 value)
  73. {
  74. struct opal_scom_map *m = map;
  75. int64_t rc;
  76. rc = opal_xscom_write(m->chip, m->addr + reg, value);
  77. return opal_xscom_err_xlate(rc);
  78. }
  79. static const struct scom_controller opal_scom_controller = {
  80. .map = opal_scom_map,
  81. .unmap = opal_scom_unmap,
  82. .read = opal_scom_read,
  83. .write = opal_scom_write
  84. };
  85. static int opal_xscom_init(void)
  86. {
  87. if (firmware_has_feature(FW_FEATURE_OPALv3))
  88. scom_init(&opal_scom_controller);
  89. return 0;
  90. }
  91. arch_initcall(opal_xscom_init);