pl172.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 (amba_part(adev) == 0x172 &&
  106. of_property_read_bool(np, "mpmc,buffer-enable"))
  107. cfg |= MPMC_STATIC_CFG_B;
  108. if (of_property_read_bool(np, "mpmc,write-protect"))
  109. cfg |= MPMC_STATIC_CFG_P;
  110. writel(cfg, pl172->base + MPMC_STATIC_CFG(cs));
  111. dev_dbg(&adev->dev, "mpmc static config cs%u: 0x%08x\n", cs, cfg);
  112. /* MPMC static memory timing */
  113. ret = pl172_timing_prop(adev, np, "mpmc,write-enable-delay",
  114. MPMC_STATIC_WAIT_WEN(cs),
  115. MPMC_STATIC_WAIT_WEN_MAX, 1);
  116. if (ret)
  117. goto fail;
  118. ret = pl172_timing_prop(adev, np, "mpmc,output-enable-delay",
  119. MPMC_STATIC_WAIT_OEN(cs),
  120. MPMC_STATIC_WAIT_OEN_MAX, 0);
  121. if (ret)
  122. goto fail;
  123. ret = pl172_timing_prop(adev, np, "mpmc,read-access-delay",
  124. MPMC_STATIC_WAIT_RD(cs),
  125. MPMC_STATIC_WAIT_RD_MAX, 1);
  126. if (ret)
  127. goto fail;
  128. ret = pl172_timing_prop(adev, np, "mpmc,page-mode-read-delay",
  129. MPMC_STATIC_WAIT_PAGE(cs),
  130. MPMC_STATIC_WAIT_PAGE_MAX, 1);
  131. if (ret)
  132. goto fail;
  133. ret = pl172_timing_prop(adev, np, "mpmc,write-access-delay",
  134. MPMC_STATIC_WAIT_WR(cs),
  135. MPMC_STATIC_WAIT_WR_MAX, 2);
  136. if (ret)
  137. goto fail;
  138. ret = pl172_timing_prop(adev, np, "mpmc,turn-round-delay",
  139. MPMC_STATIC_WAIT_TURN(cs),
  140. MPMC_STATIC_WAIT_TURN_MAX, 1);
  141. if (ret)
  142. goto fail;
  143. return 0;
  144. fail:
  145. dev_err(&adev->dev, "failed to configure cs%u\n", cs);
  146. return ret;
  147. }
  148. static int pl172_parse_cs_config(struct amba_device *adev,
  149. struct device_node *np)
  150. {
  151. u32 cs;
  152. if (!of_property_read_u32(np, "mpmc,cs", &cs)) {
  153. if (cs >= PL172_MAX_CS) {
  154. dev_err(&adev->dev, "cs%u invalid\n", cs);
  155. return -EINVAL;
  156. }
  157. return pl172_setup_static(adev, np, cs);
  158. }
  159. dev_err(&adev->dev, "cs property required\n");
  160. return -EINVAL;
  161. }
  162. static const char * const pl172_revisions[] = {"r1", "r2", "r2p3", "r2p4"};
  163. static const char * const pl175_revisions[] = {"r1"};
  164. static const char * const pl176_revisions[] = {"r0"};
  165. static int pl172_probe(struct amba_device *adev, const struct amba_id *id)
  166. {
  167. struct device_node *child_np, *np = adev->dev.of_node;
  168. struct device *dev = &adev->dev;
  169. static const char *rev = "?";
  170. struct pl172_data *pl172;
  171. int ret;
  172. if (amba_part(adev) == 0x172) {
  173. if (amba_rev(adev) < ARRAY_SIZE(pl172_revisions))
  174. rev = pl172_revisions[amba_rev(adev)];
  175. } else if (amba_part(adev) == 0x175) {
  176. if (amba_rev(adev) < ARRAY_SIZE(pl175_revisions))
  177. rev = pl175_revisions[amba_rev(adev)];
  178. } else if (amba_part(adev) == 0x176) {
  179. if (amba_rev(adev) < ARRAY_SIZE(pl176_revisions))
  180. rev = pl176_revisions[amba_rev(adev)];
  181. }
  182. dev_info(dev, "ARM PL%x revision %s\n", amba_part(adev), rev);
  183. pl172 = devm_kzalloc(dev, sizeof(*pl172), GFP_KERNEL);
  184. if (!pl172)
  185. return -ENOMEM;
  186. pl172->clk = devm_clk_get(dev, "mpmcclk");
  187. if (IS_ERR(pl172->clk)) {
  188. dev_err(dev, "no mpmcclk provided clock\n");
  189. return PTR_ERR(pl172->clk);
  190. }
  191. ret = clk_prepare_enable(pl172->clk);
  192. if (ret) {
  193. dev_err(dev, "unable to mpmcclk enable clock\n");
  194. return ret;
  195. }
  196. pl172->rate = clk_get_rate(pl172->clk) / MSEC_PER_SEC;
  197. if (!pl172->rate) {
  198. dev_err(dev, "unable to get mpmcclk clock rate\n");
  199. ret = -EINVAL;
  200. goto err_clk_enable;
  201. }
  202. ret = amba_request_regions(adev, NULL);
  203. if (ret) {
  204. dev_err(dev, "unable to request AMBA regions\n");
  205. goto err_clk_enable;
  206. }
  207. pl172->base = devm_ioremap(dev, adev->res.start,
  208. resource_size(&adev->res));
  209. if (!pl172->base) {
  210. dev_err(dev, "ioremap failed\n");
  211. ret = -ENOMEM;
  212. goto err_no_ioremap;
  213. }
  214. amba_set_drvdata(adev, pl172);
  215. /*
  216. * Loop through each child node, which represent a chip select, and
  217. * configure parameters and timing. If successful; populate devices
  218. * under that node.
  219. */
  220. for_each_available_child_of_node(np, child_np) {
  221. ret = pl172_parse_cs_config(adev, child_np);
  222. if (ret)
  223. continue;
  224. of_platform_populate(child_np, NULL, NULL, dev);
  225. }
  226. return 0;
  227. err_no_ioremap:
  228. amba_release_regions(adev);
  229. err_clk_enable:
  230. clk_disable_unprepare(pl172->clk);
  231. return ret;
  232. }
  233. static int pl172_remove(struct amba_device *adev)
  234. {
  235. struct pl172_data *pl172 = amba_get_drvdata(adev);
  236. clk_disable_unprepare(pl172->clk);
  237. amba_release_regions(adev);
  238. return 0;
  239. }
  240. static const struct amba_id pl172_ids[] = {
  241. /* PrimeCell MPMC PL172, EMC found on NXP LPC18xx and LPC43xx */
  242. {
  243. .id = 0x07041172,
  244. .mask = 0x3f0fffff,
  245. },
  246. /* PrimeCell MPMC PL175, EMC found on NXP LPC32xx */
  247. {
  248. .id = 0x07041175,
  249. .mask = 0x3f0fffff,
  250. },
  251. /* PrimeCell MPMC PL176 */
  252. {
  253. .id = 0x89041176,
  254. .mask = 0xff0fffff,
  255. },
  256. { 0, 0 },
  257. };
  258. MODULE_DEVICE_TABLE(amba, pl172_ids);
  259. static struct amba_driver pl172_driver = {
  260. .drv = {
  261. .name = "memory-pl172",
  262. },
  263. .probe = pl172_probe,
  264. .remove = pl172_remove,
  265. .id_table = pl172_ids,
  266. };
  267. module_amba_driver(pl172_driver);
  268. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  269. MODULE_DESCRIPTION("PL172 Memory Controller Driver");
  270. MODULE_LICENSE("GPL v2");