uniphier-system-bus.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/io.h>
  15. #include <linux/log2.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. /* System Bus Controller registers */
  22. #define UNIPHIER_SBC_BASE 0x100 /* base address of bank0 space */
  23. #define UNIPHIER_SBC_BASE_BE BIT(0) /* bank_enable */
  24. #define UNIPHIER_SBC_CTRL0 0x200 /* timing parameter 0 of bank0 */
  25. #define UNIPHIER_SBC_CTRL1 0x204 /* timing parameter 1 of bank0 */
  26. #define UNIPHIER_SBC_CTRL2 0x208 /* timing parameter 2 of bank0 */
  27. #define UNIPHIER_SBC_CTRL3 0x20c /* timing parameter 3 of bank0 */
  28. #define UNIPHIER_SBC_CTRL4 0x300 /* timing parameter 4 of bank0 */
  29. #define UNIPHIER_SBC_STRIDE 0x10 /* register stride to next bank */
  30. #define UNIPHIER_SBC_NR_BANKS 8 /* number of banks (chip select) */
  31. #define UNIPHIER_SBC_BASE_DUMMY 0xffffffff /* data to squash bank 0, 1 */
  32. struct uniphier_system_bus_bank {
  33. u32 base;
  34. u32 end;
  35. };
  36. struct uniphier_system_bus_priv {
  37. struct device *dev;
  38. void __iomem *membase;
  39. struct uniphier_system_bus_bank bank[UNIPHIER_SBC_NR_BANKS];
  40. };
  41. static int uniphier_system_bus_add_bank(struct uniphier_system_bus_priv *priv,
  42. int bank, u32 addr, u64 paddr, u32 size)
  43. {
  44. u64 end, mask;
  45. dev_dbg(priv->dev,
  46. "range found: bank = %d, addr = %08x, paddr = %08llx, size = %08x\n",
  47. bank, addr, paddr, size);
  48. if (bank >= ARRAY_SIZE(priv->bank)) {
  49. dev_err(priv->dev, "unsupported bank number %d\n", bank);
  50. return -EINVAL;
  51. }
  52. if (priv->bank[bank].base || priv->bank[bank].end) {
  53. dev_err(priv->dev,
  54. "range for bank %d has already been specified\n", bank);
  55. return -EINVAL;
  56. }
  57. if (paddr > U32_MAX) {
  58. dev_err(priv->dev, "base address %llx is too high\n", paddr);
  59. return -EINVAL;
  60. }
  61. end = paddr + size;
  62. if (addr > paddr) {
  63. dev_err(priv->dev,
  64. "base %08x cannot be mapped to %08llx of parent\n",
  65. addr, paddr);
  66. return -EINVAL;
  67. }
  68. paddr -= addr;
  69. paddr = round_down(paddr, 0x00020000);
  70. end = round_up(end, 0x00020000);
  71. if (end > U32_MAX) {
  72. dev_err(priv->dev, "end address %08llx is too high\n", end);
  73. return -EINVAL;
  74. }
  75. mask = paddr ^ (end - 1);
  76. mask = roundup_pow_of_two(mask);
  77. paddr = round_down(paddr, mask);
  78. end = round_up(end, mask);
  79. priv->bank[bank].base = paddr;
  80. priv->bank[bank].end = end;
  81. dev_dbg(priv->dev, "range added: bank = %d, addr = %08x, end = %08x\n",
  82. bank, priv->bank[bank].base, priv->bank[bank].end);
  83. return 0;
  84. }
  85. static int uniphier_system_bus_check_overlap(
  86. const struct uniphier_system_bus_priv *priv)
  87. {
  88. int i, j;
  89. for (i = 0; i < ARRAY_SIZE(priv->bank); i++) {
  90. for (j = i + 1; j < ARRAY_SIZE(priv->bank); j++) {
  91. if (priv->bank[i].end > priv->bank[j].base &&
  92. priv->bank[i].base < priv->bank[j].end) {
  93. dev_err(priv->dev,
  94. "region overlap between bank%d and bank%d\n",
  95. i, j);
  96. return -EINVAL;
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. static void uniphier_system_bus_check_boot_swap(
  103. struct uniphier_system_bus_priv *priv)
  104. {
  105. void __iomem *base_reg = priv->membase + UNIPHIER_SBC_BASE;
  106. int is_swapped;
  107. is_swapped = !(readl(base_reg) & UNIPHIER_SBC_BASE_BE);
  108. dev_dbg(priv->dev, "Boot Swap: %s\n", is_swapped ? "on" : "off");
  109. /*
  110. * If BOOT_SWAP was asserted on power-on-reset, the CS0 and CS1 are
  111. * swapped. In this case, bank0 and bank1 should be swapped as well.
  112. */
  113. if (is_swapped)
  114. swap(priv->bank[0], priv->bank[1]);
  115. }
  116. static void uniphier_system_bus_set_reg(
  117. const struct uniphier_system_bus_priv *priv)
  118. {
  119. void __iomem *base_reg = priv->membase + UNIPHIER_SBC_BASE;
  120. u32 base, end, mask, val;
  121. int i;
  122. for (i = 0; i < ARRAY_SIZE(priv->bank); i++) {
  123. base = priv->bank[i].base;
  124. end = priv->bank[i].end;
  125. if (base == end) {
  126. /*
  127. * If SBC_BASE0 or SBC_BASE1 is set to zero, the access
  128. * to anywhere in the system bus space is routed to
  129. * bank 0 (if boot swap if off) or bank 1 (if boot swap
  130. * if on). It means that CPUs cannot get access to
  131. * bank 2 or later. In other words, bank 0/1 cannot
  132. * be disabled even if its bank_enable bits is cleared.
  133. * This seems odd, but it is how this hardware goes.
  134. * As a workaround, dummy data (0xffffffff) should be
  135. * set when the bank 0/1 is unused. As for bank 2 and
  136. * later, they can be simply disable by clearing the
  137. * bank_enable bit.
  138. */
  139. if (i < 2)
  140. val = UNIPHIER_SBC_BASE_DUMMY;
  141. else
  142. val = 0;
  143. } else {
  144. mask = base ^ (end - 1);
  145. val = base & 0xfffe0000;
  146. val |= (~mask >> 16) & 0xfffe;
  147. val |= UNIPHIER_SBC_BASE_BE;
  148. }
  149. dev_dbg(priv->dev, "SBC_BASE[%d] = 0x%08x\n", i, val);
  150. writel(val, base_reg + UNIPHIER_SBC_STRIDE * i);
  151. }
  152. }
  153. static int uniphier_system_bus_probe(struct platform_device *pdev)
  154. {
  155. struct device *dev = &pdev->dev;
  156. struct uniphier_system_bus_priv *priv;
  157. struct resource *regs;
  158. const __be32 *ranges;
  159. u32 cells, addr, size;
  160. u64 paddr;
  161. int pna, bank, rlen, rone, ret;
  162. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  163. if (!priv)
  164. return -ENOMEM;
  165. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. priv->membase = devm_ioremap_resource(dev, regs);
  167. if (IS_ERR(priv->membase))
  168. return PTR_ERR(priv->membase);
  169. priv->dev = dev;
  170. pna = of_n_addr_cells(dev->of_node);
  171. ret = of_property_read_u32(dev->of_node, "#address-cells", &cells);
  172. if (ret) {
  173. dev_err(dev, "failed to get #address-cells\n");
  174. return ret;
  175. }
  176. if (cells != 2) {
  177. dev_err(dev, "#address-cells must be 2\n");
  178. return -EINVAL;
  179. }
  180. ret = of_property_read_u32(dev->of_node, "#size-cells", &cells);
  181. if (ret) {
  182. dev_err(dev, "failed to get #size-cells\n");
  183. return ret;
  184. }
  185. if (cells != 1) {
  186. dev_err(dev, "#size-cells must be 1\n");
  187. return -EINVAL;
  188. }
  189. ranges = of_get_property(dev->of_node, "ranges", &rlen);
  190. if (!ranges) {
  191. dev_err(dev, "failed to get ranges property\n");
  192. return -ENOENT;
  193. }
  194. rlen /= sizeof(*ranges);
  195. rone = pna + 2;
  196. for (; rlen >= rone; rlen -= rone) {
  197. bank = be32_to_cpup(ranges++);
  198. addr = be32_to_cpup(ranges++);
  199. paddr = of_translate_address(dev->of_node, ranges);
  200. if (paddr == OF_BAD_ADDR)
  201. return -EINVAL;
  202. ranges += pna;
  203. size = be32_to_cpup(ranges++);
  204. ret = uniphier_system_bus_add_bank(priv, bank, addr,
  205. paddr, size);
  206. if (ret)
  207. return ret;
  208. }
  209. ret = uniphier_system_bus_check_overlap(priv);
  210. if (ret)
  211. return ret;
  212. uniphier_system_bus_check_boot_swap(priv);
  213. uniphier_system_bus_set_reg(priv);
  214. /* Now, the bus is configured. Populate platform_devices below it */
  215. return of_platform_default_populate(dev->of_node, NULL, dev);
  216. }
  217. static const struct of_device_id uniphier_system_bus_match[] = {
  218. { .compatible = "socionext,uniphier-system-bus" },
  219. { /* sentinel */ }
  220. };
  221. MODULE_DEVICE_TABLE(of, uniphier_system_bus_match);
  222. static struct platform_driver uniphier_system_bus_driver = {
  223. .probe = uniphier_system_bus_probe,
  224. .driver = {
  225. .name = "uniphier-system-bus",
  226. .of_match_table = uniphier_system_bus_match,
  227. },
  228. };
  229. module_platform_driver(uniphier_system_bus_driver);
  230. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  231. MODULE_DESCRIPTION("UniPhier System Bus driver");
  232. MODULE_LICENSE("GPL");