pl172.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Memory controller driver for ARM PrimeCell PL172
  3. * PrimeCell MultiPort Memory Controller (PL172)
  4. *
  5. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  6. *
  7. * Based on:
  8. * TI AEMIF driver, Copyright (C) 2010 - 2013 Texas Instruments Inc.
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/amba/bus.h>
  15. #include <linux/clk.h>
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/time.h>
  25. #define MPMC_STATIC_CFG(n) (0x200 + 0x20 * n)
  26. #define MPMC_STATIC_CFG_MW_8BIT 0x0
  27. #define MPMC_STATIC_CFG_MW_16BIT 0x1
  28. #define MPMC_STATIC_CFG_MW_32BIT 0x2
  29. #define MPMC_STATIC_CFG_PM BIT(3)
  30. #define MPMC_STATIC_CFG_PC BIT(6)
  31. #define MPMC_STATIC_CFG_PB BIT(7)
  32. #define MPMC_STATIC_CFG_EW BIT(8)
  33. #define MPMC_STATIC_CFG_B BIT(19)
  34. #define MPMC_STATIC_CFG_P BIT(20)
  35. #define MPMC_STATIC_WAIT_WEN(n) (0x204 + 0x20 * n)
  36. #define MPMC_STATIC_WAIT_WEN_MAX 0x0f
  37. #define MPMC_STATIC_WAIT_OEN(n) (0x208 + 0x20 * n)
  38. #define MPMC_STATIC_WAIT_OEN_MAX 0x0f
  39. #define MPMC_STATIC_WAIT_RD(n) (0x20c + 0x20 * n)
  40. #define MPMC_STATIC_WAIT_RD_MAX 0x1f
  41. #define MPMC_STATIC_WAIT_PAGE(n) (0x210 + 0x20 * n)
  42. #define MPMC_STATIC_WAIT_PAGE_MAX 0x1f
  43. #define MPMC_STATIC_WAIT_WR(n) (0x214 + 0x20 * n)
  44. #define MPMC_STATIC_WAIT_WR_MAX 0x1f
  45. #define MPMC_STATIC_WAIT_TURN(n) (0x218 + 0x20 * n)
  46. #define MPMC_STATIC_WAIT_TURN_MAX 0x0f
  47. /* Maximum number of static chip selects */
  48. #define PL172_MAX_CS 4
  49. struct pl172_data {
  50. void __iomem *base;
  51. unsigned long rate;
  52. struct clk *clk;
  53. };
  54. static int pl172_timing_prop(struct amba_device *adev,
  55. const struct device_node *np, const char *name,
  56. u32 reg_offset, u32 max, int start)
  57. {
  58. struct pl172_data *pl172 = amba_get_drvdata(adev);
  59. int cycles;
  60. u32 val;
  61. if (!of_property_read_u32(np, name, &val)) {
  62. cycles = DIV_ROUND_UP(val * pl172->rate, NSEC_PER_MSEC) - start;
  63. if (cycles < 0) {
  64. cycles = 0;
  65. } else if (cycles > max) {
  66. dev_err(&adev->dev, "%s timing too tight\n", name);
  67. return -EINVAL;
  68. }
  69. writel(cycles, pl172->base + reg_offset);
  70. }
  71. dev_dbg(&adev->dev, "%s: %u cycle(s)\n", name, start +
  72. readl(pl172->base + reg_offset));
  73. return 0;
  74. }
  75. static int pl172_setup_static(struct amba_device *adev,
  76. struct device_node *np, u32 cs)
  77. {
  78. struct pl172_data *pl172 = amba_get_drvdata(adev);
  79. u32 cfg;
  80. int ret;
  81. /* MPMC static memory configuration */
  82. if (!of_property_read_u32(np, "mpmc,memory-width", &cfg)) {
  83. if (cfg == 8) {
  84. cfg = MPMC_STATIC_CFG_MW_8BIT;
  85. } else if (cfg == 16) {
  86. cfg = MPMC_STATIC_CFG_MW_16BIT;
  87. } else if (cfg == 32) {
  88. cfg = MPMC_STATIC_CFG_MW_32BIT;
  89. } else {
  90. dev_err(&adev->dev, "invalid memory width cs%u\n", cs);
  91. return -EINVAL;
  92. }
  93. } else {
  94. dev_err(&adev->dev, "memory-width property required\n");
  95. return -EINVAL;
  96. }
  97. if (of_property_read_bool(np, "mpmc,async-page-mode"))
  98. cfg |= MPMC_STATIC_CFG_PM;
  99. if (of_property_read_bool(np, "mpmc,cs-active-high"))
  100. cfg |= MPMC_STATIC_CFG_PC;
  101. if (of_property_read_bool(np, "mpmc,byte-lane-low"))
  102. cfg |= MPMC_STATIC_CFG_PB;
  103. if (of_property_read_bool(np, "mpmc,extended-wait"))
  104. cfg |= MPMC_STATIC_CFG_EW;
  105. if (of_property_read_bool(np, "mpmc,buffer-enable"))
  106. cfg |= MPMC_STATIC_CFG_B;
  107. if (of_property_read_bool(np, "mpmc,write-protect"))
  108. cfg |= MPMC_STATIC_CFG_P;
  109. writel(cfg, pl172->base + MPMC_STATIC_CFG(cs));
  110. dev_dbg(&adev->dev, "mpmc static config cs%u: 0x%08x\n", cs, cfg);
  111. /* MPMC static memory timing */
  112. ret = pl172_timing_prop(adev, np, "mpmc,write-enable-delay",
  113. MPMC_STATIC_WAIT_WEN(cs),
  114. MPMC_STATIC_WAIT_WEN_MAX, 1);
  115. if (ret)
  116. goto fail;
  117. ret = pl172_timing_prop(adev, np, "mpmc,output-enable-delay",
  118. MPMC_STATIC_WAIT_OEN(cs),
  119. MPMC_STATIC_WAIT_OEN_MAX, 0);
  120. if (ret)
  121. goto fail;
  122. ret = pl172_timing_prop(adev, np, "mpmc,read-access-delay",
  123. MPMC_STATIC_WAIT_RD(cs),
  124. MPMC_STATIC_WAIT_RD_MAX, 1);
  125. if (ret)
  126. goto fail;
  127. ret = pl172_timing_prop(adev, np, "mpmc,page-mode-read-delay",
  128. MPMC_STATIC_WAIT_PAGE(cs),
  129. MPMC_STATIC_WAIT_PAGE_MAX, 1);
  130. if (ret)
  131. goto fail;
  132. ret = pl172_timing_prop(adev, np, "mpmc,write-access-delay",
  133. MPMC_STATIC_WAIT_WR(cs),
  134. MPMC_STATIC_WAIT_WR_MAX, 2);
  135. if (ret)
  136. goto fail;
  137. ret = pl172_timing_prop(adev, np, "mpmc,turn-round-delay",
  138. MPMC_STATIC_WAIT_TURN(cs),
  139. MPMC_STATIC_WAIT_TURN_MAX, 1);
  140. if (ret)
  141. goto fail;
  142. return 0;
  143. fail:
  144. dev_err(&adev->dev, "failed to configure cs%u\n", cs);
  145. return ret;
  146. }
  147. static int pl172_parse_cs_config(struct amba_device *adev,
  148. struct device_node *np)
  149. {
  150. u32 cs;
  151. if (!of_property_read_u32(np, "mpmc,cs", &cs)) {
  152. if (cs >= PL172_MAX_CS) {
  153. dev_err(&adev->dev, "cs%u invalid\n", cs);
  154. return -EINVAL;
  155. }
  156. return pl172_setup_static(adev, np, cs);
  157. }
  158. dev_err(&adev->dev, "cs property required\n");
  159. return -EINVAL;
  160. }
  161. static const char * const pl172_revisions[] = {"r1", "r2", "r2p3", "r2p4"};
  162. static int pl172_probe(struct amba_device *adev, const struct amba_id *id)
  163. {
  164. struct device_node *child_np, *np = adev->dev.of_node;
  165. struct device *dev = &adev->dev;
  166. static const char *rev = "?";
  167. struct pl172_data *pl172;
  168. int ret;
  169. if (amba_part(adev) == 0x172) {
  170. if (amba_rev(adev) < ARRAY_SIZE(pl172_revisions))
  171. rev = pl172_revisions[amba_rev(adev)];
  172. }
  173. dev_info(dev, "ARM PL%x revision %s\n", amba_part(adev), rev);
  174. pl172 = devm_kzalloc(dev, sizeof(*pl172), GFP_KERNEL);
  175. if (!pl172)
  176. return -ENOMEM;
  177. pl172->clk = devm_clk_get(dev, "mpmcclk");
  178. if (IS_ERR(pl172->clk)) {
  179. dev_err(dev, "no mpmcclk provided clock\n");
  180. return PTR_ERR(pl172->clk);
  181. }
  182. ret = clk_prepare_enable(pl172->clk);
  183. if (ret) {
  184. dev_err(dev, "unable to mpmcclk enable clock\n");
  185. return ret;
  186. }
  187. pl172->rate = clk_get_rate(pl172->clk) / MSEC_PER_SEC;
  188. if (!pl172->rate) {
  189. dev_err(dev, "unable to get mpmcclk clock rate\n");
  190. ret = -EINVAL;
  191. goto err_clk_enable;
  192. }
  193. ret = amba_request_regions(adev, NULL);
  194. if (ret) {
  195. dev_err(dev, "unable to request AMBA regions\n");
  196. goto err_clk_enable;
  197. }
  198. pl172->base = devm_ioremap(dev, adev->res.start,
  199. resource_size(&adev->res));
  200. if (!pl172->base) {
  201. dev_err(dev, "ioremap failed\n");
  202. ret = -ENOMEM;
  203. goto err_no_ioremap;
  204. }
  205. amba_set_drvdata(adev, pl172);
  206. /*
  207. * Loop through each child node, which represent a chip select, and
  208. * configure parameters and timing. If successful; populate devices
  209. * under that node.
  210. */
  211. for_each_available_child_of_node(np, child_np) {
  212. ret = pl172_parse_cs_config(adev, child_np);
  213. if (ret)
  214. continue;
  215. of_platform_populate(child_np, NULL, NULL, dev);
  216. }
  217. return 0;
  218. err_no_ioremap:
  219. amba_release_regions(adev);
  220. err_clk_enable:
  221. clk_disable_unprepare(pl172->clk);
  222. return ret;
  223. }
  224. static int pl172_remove(struct amba_device *adev)
  225. {
  226. struct pl172_data *pl172 = amba_get_drvdata(adev);
  227. clk_disable_unprepare(pl172->clk);
  228. amba_release_regions(adev);
  229. return 0;
  230. }
  231. static const struct amba_id pl172_ids[] = {
  232. {
  233. .id = 0x07341172,
  234. .mask = 0xffffffff,
  235. },
  236. { 0, 0 },
  237. };
  238. MODULE_DEVICE_TABLE(amba, pl172_ids);
  239. static struct amba_driver pl172_driver = {
  240. .drv = {
  241. .name = "memory-pl172",
  242. },
  243. .probe = pl172_probe,
  244. .remove = pl172_remove,
  245. .id_table = pl172_ids,
  246. };
  247. module_amba_driver(pl172_driver);
  248. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  249. MODULE_DESCRIPTION("PL172 Memory Controller Driver");
  250. MODULE_LICENSE("GPL v2");