loongson2_guts.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Author: Yinbo Zhu <zhuyinbo@loongson.cn>
  4. * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
  5. */
  6. #include <linux/io.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/of_fdt.h>
  10. #include <linux/sys_soc.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. static struct soc_device_attribute soc_dev_attr;
  14. static struct soc_device *soc_dev;
  15. /*
  16. * Global Utility Registers.
  17. *
  18. * Not all registers defined in this structure are available on all chips, so
  19. * you are expected to know whether a given register actually exists on your
  20. * chip before you access it.
  21. *
  22. * Also, some registers are similar on different chips but have slightly
  23. * different names. In these cases, one name is chosen to avoid extraneous
  24. * #ifdefs.
  25. */
  26. struct scfg_guts {
  27. u32 svr; /* Version Register */
  28. u8 res0[4];
  29. u16 feature; /* Feature Register */
  30. u32 vendor; /* Vendor Register */
  31. u8 res1[6];
  32. u32 id;
  33. u8 res2[0x3ff8 - 0x18];
  34. u32 chip;
  35. };
  36. static struct guts {
  37. struct scfg_guts __iomem *regs;
  38. bool little_endian;
  39. } *guts;
  40. struct loongson2_soc_die_attr {
  41. char *die;
  42. u32 svr;
  43. u32 mask;
  44. };
  45. /* SoC die attribute definition for Loongson-2 platform */
  46. static const struct loongson2_soc_die_attr loongson2_soc_die[] = {
  47. /*
  48. * LoongArch-based SoCs Loongson-2 Series
  49. */
  50. /* Die: 2k1000, SoC: 2k1000 */
  51. { .die = "2K1000",
  52. .svr = 0x00000013,
  53. .mask = 0x000000ff,
  54. },
  55. { },
  56. };
  57. static const struct loongson2_soc_die_attr *loongson2_soc_die_match(
  58. u32 svr, const struct loongson2_soc_die_attr *matches)
  59. {
  60. while (matches->svr) {
  61. if (matches->svr == (svr & matches->mask))
  62. return matches;
  63. matches++;
  64. }
  65. return NULL;
  66. }
  67. static u32 loongson2_guts_get_svr(void)
  68. {
  69. u32 svr = 0;
  70. if (!guts || !guts->regs)
  71. return svr;
  72. if (guts->little_endian)
  73. svr = ioread32(&guts->regs->svr);
  74. else
  75. svr = ioread32be(&guts->regs->svr);
  76. return svr;
  77. }
  78. static int loongson2_guts_probe(struct platform_device *pdev)
  79. {
  80. struct device_node *root, *np = pdev->dev.of_node;
  81. struct device *dev = &pdev->dev;
  82. const struct loongson2_soc_die_attr *soc_die;
  83. const char *machine;
  84. u32 svr;
  85. /* Initialize guts */
  86. guts = devm_kzalloc(dev, sizeof(*guts), GFP_KERNEL);
  87. if (!guts)
  88. return -ENOMEM;
  89. guts->little_endian = of_property_read_bool(np, "little-endian");
  90. guts->regs = devm_platform_ioremap_resource(pdev, 0);
  91. if (IS_ERR(guts->regs))
  92. return PTR_ERR(guts->regs);
  93. /* Register soc device */
  94. root = of_find_node_by_path("/");
  95. if (of_property_read_string(root, "model", &machine))
  96. of_property_read_string_index(root, "compatible", 0, &machine);
  97. of_node_put(root);
  98. if (machine)
  99. soc_dev_attr.machine = devm_kstrdup(dev, machine, GFP_KERNEL);
  100. svr = loongson2_guts_get_svr();
  101. soc_die = loongson2_soc_die_match(svr, loongson2_soc_die);
  102. if (soc_die) {
  103. soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL,
  104. "Loongson %s", soc_die->die);
  105. } else {
  106. soc_dev_attr.family = devm_kasprintf(dev, GFP_KERNEL, "Loongson");
  107. }
  108. if (!soc_dev_attr.family)
  109. return -ENOMEM;
  110. soc_dev_attr.soc_id = devm_kasprintf(dev, GFP_KERNEL,
  111. "svr:0x%08x", svr);
  112. if (!soc_dev_attr.soc_id)
  113. return -ENOMEM;
  114. soc_dev_attr.revision = devm_kasprintf(dev, GFP_KERNEL, "%d.%d",
  115. (svr >> 4) & 0xf, svr & 0xf);
  116. if (!soc_dev_attr.revision)
  117. return -ENOMEM;
  118. soc_dev = soc_device_register(&soc_dev_attr);
  119. if (IS_ERR(soc_dev))
  120. return PTR_ERR(soc_dev);
  121. pr_info("Machine: %s\n", soc_dev_attr.machine);
  122. pr_info("SoC family: %s\n", soc_dev_attr.family);
  123. pr_info("SoC ID: %s, Revision: %s\n",
  124. soc_dev_attr.soc_id, soc_dev_attr.revision);
  125. return 0;
  126. }
  127. static void loongson2_guts_remove(struct platform_device *dev)
  128. {
  129. soc_device_unregister(soc_dev);
  130. }
  131. /*
  132. * Table for matching compatible strings, for device tree
  133. * guts node, for Loongson-2 SoCs.
  134. */
  135. static const struct of_device_id loongson2_guts_of_match[] = {
  136. { .compatible = "loongson,ls2k-chipid", },
  137. {}
  138. };
  139. MODULE_DEVICE_TABLE(of, loongson2_guts_of_match);
  140. static struct platform_driver loongson2_guts_driver = {
  141. .driver = {
  142. .name = "loongson2-guts",
  143. .of_match_table = loongson2_guts_of_match,
  144. },
  145. .probe = loongson2_guts_probe,
  146. .remove_new = loongson2_guts_remove,
  147. };
  148. static int __init loongson2_guts_init(void)
  149. {
  150. return platform_driver_register(&loongson2_guts_driver);
  151. }
  152. core_initcall(loongson2_guts_init);
  153. static void __exit loongson2_guts_exit(void)
  154. {
  155. platform_driver_unregister(&loongson2_guts_driver);
  156. }
  157. module_exit(loongson2_guts_exit);
  158. MODULE_DESCRIPTION("Loongson2 GUTS driver");
  159. MODULE_LICENSE("GPL");