dfl-emif.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DFL device driver for EMIF private feature
  4. *
  5. * Copyright (C) 2020 Intel Corporation, Inc.
  6. *
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/dfl.h>
  10. #include <linux/errno.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/io-64-nonatomic-lo-hi.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/types.h>
  18. #define FME_FEATURE_ID_EMIF 0x9
  19. #define EMIF_STAT 0x8
  20. #define EMIF_STAT_INIT_DONE_SFT 0
  21. #define EMIF_STAT_CALC_FAIL_SFT 8
  22. #define EMIF_STAT_CLEAR_BUSY_SFT 16
  23. #define EMIF_CTRL 0x10
  24. #define EMIF_CTRL_CLEAR_EN_SFT 0
  25. #define EMIF_CTRL_CLEAR_EN_MSK GENMASK_ULL(7, 0)
  26. #define EMIF_POLL_INVL 10000 /* us */
  27. #define EMIF_POLL_TIMEOUT 5000000 /* us */
  28. /*
  29. * The Capability Register replaces the Control Register (at the same
  30. * offset) for EMIF feature revisions > 0. The bitmask that indicates
  31. * the presence of memory channels exists in both the Capability Register
  32. * and Control Register definitions. These can be thought of as a C union.
  33. * The Capability Register definitions are used to check for the existence
  34. * of a memory channel, and the Control Register definitions are used for
  35. * managing the memory-clear functionality in revision 0.
  36. */
  37. #define EMIF_CAPABILITY_BASE 0x10
  38. #define EMIF_CAPABILITY_CHN_MSK_V0 GENMASK_ULL(3, 0)
  39. #define EMIF_CAPABILITY_CHN_MSK GENMASK_ULL(7, 0)
  40. struct dfl_emif {
  41. struct device *dev;
  42. void __iomem *base;
  43. spinlock_t lock; /* Serialises access to EMIF_CTRL reg */
  44. };
  45. struct emif_attr {
  46. struct device_attribute attr;
  47. u32 shift;
  48. u32 index;
  49. };
  50. #define to_emif_attr(dev_attr) \
  51. container_of(dev_attr, struct emif_attr, attr)
  52. static ssize_t emif_state_show(struct device *dev,
  53. struct device_attribute *attr, char *buf)
  54. {
  55. struct emif_attr *eattr = to_emif_attr(attr);
  56. struct dfl_emif *de = dev_get_drvdata(dev);
  57. u64 val;
  58. val = readq(de->base + EMIF_STAT);
  59. return sysfs_emit(buf, "%u\n",
  60. !!(val & BIT_ULL(eattr->shift + eattr->index)));
  61. }
  62. static ssize_t emif_clear_store(struct device *dev,
  63. struct device_attribute *attr,
  64. const char *buf, size_t count)
  65. {
  66. struct emif_attr *eattr = to_emif_attr(attr);
  67. struct dfl_emif *de = dev_get_drvdata(dev);
  68. u64 clear_busy_msk, clear_en_msk, val;
  69. void __iomem *base = de->base;
  70. if (!sysfs_streq(buf, "1"))
  71. return -EINVAL;
  72. clear_busy_msk = BIT_ULL(EMIF_STAT_CLEAR_BUSY_SFT + eattr->index);
  73. clear_en_msk = BIT_ULL(EMIF_CTRL_CLEAR_EN_SFT + eattr->index);
  74. spin_lock(&de->lock);
  75. /* The CLEAR_EN field is WO, but other fields are RW */
  76. val = readq(base + EMIF_CTRL);
  77. val &= ~EMIF_CTRL_CLEAR_EN_MSK;
  78. val |= clear_en_msk;
  79. writeq(val, base + EMIF_CTRL);
  80. spin_unlock(&de->lock);
  81. if (readq_poll_timeout(base + EMIF_STAT, val,
  82. !(val & clear_busy_msk),
  83. EMIF_POLL_INVL, EMIF_POLL_TIMEOUT)) {
  84. dev_err(de->dev, "timeout, fail to clear\n");
  85. return -ETIMEDOUT;
  86. }
  87. return count;
  88. }
  89. #define emif_state_attr(_name, _shift, _index) \
  90. static struct emif_attr emif_attr_##inf##_index##_##_name = \
  91. { .attr = __ATTR(inf##_index##_##_name, 0444, \
  92. emif_state_show, NULL), \
  93. .shift = (_shift), .index = (_index) }
  94. #define emif_clear_attr(_index) \
  95. static struct emif_attr emif_attr_##inf##_index##_clear = \
  96. { .attr = __ATTR(inf##_index##_clear, 0200, \
  97. NULL, emif_clear_store), \
  98. .index = (_index) }
  99. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 0);
  100. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 1);
  101. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 2);
  102. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 3);
  103. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 4);
  104. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 5);
  105. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 6);
  106. emif_state_attr(init_done, EMIF_STAT_INIT_DONE_SFT, 7);
  107. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 0);
  108. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 1);
  109. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 2);
  110. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 3);
  111. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 4);
  112. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 5);
  113. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 6);
  114. emif_state_attr(cal_fail, EMIF_STAT_CALC_FAIL_SFT, 7);
  115. emif_clear_attr(0);
  116. emif_clear_attr(1);
  117. emif_clear_attr(2);
  118. emif_clear_attr(3);
  119. emif_clear_attr(4);
  120. emif_clear_attr(5);
  121. emif_clear_attr(6);
  122. emif_clear_attr(7);
  123. static struct attribute *dfl_emif_attrs[] = {
  124. &emif_attr_inf0_init_done.attr.attr,
  125. &emif_attr_inf0_cal_fail.attr.attr,
  126. &emif_attr_inf0_clear.attr.attr,
  127. &emif_attr_inf1_init_done.attr.attr,
  128. &emif_attr_inf1_cal_fail.attr.attr,
  129. &emif_attr_inf1_clear.attr.attr,
  130. &emif_attr_inf2_init_done.attr.attr,
  131. &emif_attr_inf2_cal_fail.attr.attr,
  132. &emif_attr_inf2_clear.attr.attr,
  133. &emif_attr_inf3_init_done.attr.attr,
  134. &emif_attr_inf3_cal_fail.attr.attr,
  135. &emif_attr_inf3_clear.attr.attr,
  136. &emif_attr_inf4_init_done.attr.attr,
  137. &emif_attr_inf4_cal_fail.attr.attr,
  138. &emif_attr_inf4_clear.attr.attr,
  139. &emif_attr_inf5_init_done.attr.attr,
  140. &emif_attr_inf5_cal_fail.attr.attr,
  141. &emif_attr_inf5_clear.attr.attr,
  142. &emif_attr_inf6_init_done.attr.attr,
  143. &emif_attr_inf6_cal_fail.attr.attr,
  144. &emif_attr_inf6_clear.attr.attr,
  145. &emif_attr_inf7_init_done.attr.attr,
  146. &emif_attr_inf7_cal_fail.attr.attr,
  147. &emif_attr_inf7_clear.attr.attr,
  148. NULL,
  149. };
  150. static umode_t dfl_emif_visible(struct kobject *kobj,
  151. struct attribute *attr, int n)
  152. {
  153. struct dfl_emif *de = dev_get_drvdata(kobj_to_dev(kobj));
  154. struct emif_attr *eattr = container_of(attr, struct emif_attr,
  155. attr.attr);
  156. struct dfl_device *ddev = to_dfl_dev(de->dev);
  157. u64 val;
  158. /*
  159. * This device supports up to 8 memory interfaces, but not all
  160. * interfaces are used on different platforms. The read out value of
  161. * CAPABILITY_CHN_MSK field (which is a bitmap) indicates which
  162. * interfaces are available.
  163. */
  164. if (ddev->revision > 0 && strstr(attr->name, "_clear"))
  165. return 0;
  166. if (ddev->revision == 0)
  167. val = FIELD_GET(EMIF_CAPABILITY_CHN_MSK_V0,
  168. readq(de->base + EMIF_CAPABILITY_BASE));
  169. else
  170. val = FIELD_GET(EMIF_CAPABILITY_CHN_MSK,
  171. readq(de->base + EMIF_CAPABILITY_BASE));
  172. return (val & BIT_ULL(eattr->index)) ? attr->mode : 0;
  173. }
  174. static const struct attribute_group dfl_emif_group = {
  175. .is_visible = dfl_emif_visible,
  176. .attrs = dfl_emif_attrs,
  177. };
  178. static const struct attribute_group *dfl_emif_groups[] = {
  179. &dfl_emif_group,
  180. NULL,
  181. };
  182. static int dfl_emif_probe(struct dfl_device *ddev)
  183. {
  184. struct device *dev = &ddev->dev;
  185. struct dfl_emif *de;
  186. de = devm_kzalloc(dev, sizeof(*de), GFP_KERNEL);
  187. if (!de)
  188. return -ENOMEM;
  189. de->base = devm_ioremap_resource(dev, &ddev->mmio_res);
  190. if (IS_ERR(de->base))
  191. return PTR_ERR(de->base);
  192. de->dev = dev;
  193. spin_lock_init(&de->lock);
  194. dev_set_drvdata(dev, de);
  195. return 0;
  196. }
  197. static const struct dfl_device_id dfl_emif_ids[] = {
  198. { FME_ID, FME_FEATURE_ID_EMIF },
  199. { }
  200. };
  201. MODULE_DEVICE_TABLE(dfl, dfl_emif_ids);
  202. static struct dfl_driver dfl_emif_driver = {
  203. .drv = {
  204. .name = "dfl-emif",
  205. .dev_groups = dfl_emif_groups,
  206. },
  207. .id_table = dfl_emif_ids,
  208. .probe = dfl_emif_probe,
  209. };
  210. module_dfl_driver(dfl_emif_driver);
  211. MODULE_DESCRIPTION("DFL EMIF driver");
  212. MODULE_AUTHOR("Intel Corporation");
  213. MODULE_LICENSE("GPL v2");