bman_ccsr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Copyright (c) 2009 - 2016 Freescale Semiconductor, Inc.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * * Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * * Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. * * Neither the name of Freescale Semiconductor nor the
  11. * names of its contributors may be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * ALTERNATIVELY, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL") as published by the Free Software
  16. * Foundation, either version 2 of that License or (at your option) any
  17. * later version.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "bman_priv.h"
  31. u16 bman_ip_rev;
  32. EXPORT_SYMBOL(bman_ip_rev);
  33. /* Register offsets */
  34. #define REG_FBPR_FPC 0x0800
  35. #define REG_ECSR 0x0a00
  36. #define REG_ECIR 0x0a04
  37. #define REG_EADR 0x0a08
  38. #define REG_EDATA(n) (0x0a10 + ((n) * 0x04))
  39. #define REG_SBEC(n) (0x0a80 + ((n) * 0x04))
  40. #define REG_IP_REV_1 0x0bf8
  41. #define REG_IP_REV_2 0x0bfc
  42. #define REG_FBPR_BARE 0x0c00
  43. #define REG_FBPR_BAR 0x0c04
  44. #define REG_FBPR_AR 0x0c10
  45. #define REG_SRCIDR 0x0d04
  46. #define REG_LIODNR 0x0d08
  47. #define REG_ERR_ISR 0x0e00
  48. #define REG_ERR_IER 0x0e04
  49. #define REG_ERR_ISDR 0x0e08
  50. /* Used by all error interrupt registers except 'inhibit' */
  51. #define BM_EIRQ_IVCI 0x00000010 /* Invalid Command Verb */
  52. #define BM_EIRQ_FLWI 0x00000008 /* FBPR Low Watermark */
  53. #define BM_EIRQ_MBEI 0x00000004 /* Multi-bit ECC Error */
  54. #define BM_EIRQ_SBEI 0x00000002 /* Single-bit ECC Error */
  55. #define BM_EIRQ_BSCN 0x00000001 /* pool State Change Notification */
  56. struct bman_hwerr_txt {
  57. u32 mask;
  58. const char *txt;
  59. };
  60. static const struct bman_hwerr_txt bman_hwerr_txts[] = {
  61. { BM_EIRQ_IVCI, "Invalid Command Verb" },
  62. { BM_EIRQ_FLWI, "FBPR Low Watermark" },
  63. { BM_EIRQ_MBEI, "Multi-bit ECC Error" },
  64. { BM_EIRQ_SBEI, "Single-bit ECC Error" },
  65. { BM_EIRQ_BSCN, "Pool State Change Notification" },
  66. };
  67. /* Only trigger low water mark interrupt once only */
  68. #define BMAN_ERRS_TO_DISABLE BM_EIRQ_FLWI
  69. /* Pointer to the start of the BMan's CCSR space */
  70. static u32 __iomem *bm_ccsr_start;
  71. static inline u32 bm_ccsr_in(u32 offset)
  72. {
  73. return ioread32be(bm_ccsr_start + offset/4);
  74. }
  75. static inline void bm_ccsr_out(u32 offset, u32 val)
  76. {
  77. iowrite32be(val, bm_ccsr_start + offset/4);
  78. }
  79. static void bm_get_version(u16 *id, u8 *major, u8 *minor)
  80. {
  81. u32 v = bm_ccsr_in(REG_IP_REV_1);
  82. *id = (v >> 16);
  83. *major = (v >> 8) & 0xff;
  84. *minor = v & 0xff;
  85. }
  86. /* signal transactions for FBPRs with higher priority */
  87. #define FBPR_AR_RPRIO_HI BIT(30)
  88. /* Track if probe has occurred and if cleanup is required */
  89. static int __bman_probed;
  90. static int __bman_requires_cleanup;
  91. static int bm_set_memory(u64 ba, u32 size)
  92. {
  93. u32 bar, bare;
  94. u32 exp = ilog2(size);
  95. /* choke if size isn't within range */
  96. DPAA_ASSERT(size >= 4096 && size <= 1024*1024*1024 &&
  97. is_power_of_2(size));
  98. /* choke if '[e]ba' has lower-alignment than 'size' */
  99. DPAA_ASSERT(!(ba & (size - 1)));
  100. /* Check to see if BMan has already been initialized */
  101. bar = bm_ccsr_in(REG_FBPR_BAR);
  102. if (bar) {
  103. /* Maker sure ba == what was programmed) */
  104. bare = bm_ccsr_in(REG_FBPR_BARE);
  105. if (bare != upper_32_bits(ba) || bar != lower_32_bits(ba)) {
  106. pr_err("Attempted to reinitialize BMan with different BAR, got 0x%llx read BARE=0x%x BAR=0x%x\n",
  107. ba, bare, bar);
  108. return -ENOMEM;
  109. }
  110. pr_info("BMan BAR already configured\n");
  111. __bman_requires_cleanup = 1;
  112. return 1;
  113. }
  114. bm_ccsr_out(REG_FBPR_BARE, upper_32_bits(ba));
  115. bm_ccsr_out(REG_FBPR_BAR, lower_32_bits(ba));
  116. bm_ccsr_out(REG_FBPR_AR, exp - 1);
  117. return 0;
  118. }
  119. /*
  120. * Location and size of BMan private memory
  121. *
  122. * Ideally we would use the DMA API to turn rmem->base into a DMA address
  123. * (especially if iommu translations ever get involved). Unfortunately, the
  124. * DMA API currently does not allow mapping anything that is not backed with
  125. * a struct page.
  126. */
  127. static dma_addr_t fbpr_a;
  128. static size_t fbpr_sz;
  129. static int bman_fbpr(struct reserved_mem *rmem)
  130. {
  131. fbpr_a = rmem->base;
  132. fbpr_sz = rmem->size;
  133. WARN_ON(!(fbpr_a && fbpr_sz));
  134. return 0;
  135. }
  136. RESERVEDMEM_OF_DECLARE(bman_fbpr, "fsl,bman-fbpr", bman_fbpr);
  137. static irqreturn_t bman_isr(int irq, void *ptr)
  138. {
  139. u32 isr_val, ier_val, ecsr_val, isr_mask, i;
  140. struct device *dev = ptr;
  141. ier_val = bm_ccsr_in(REG_ERR_IER);
  142. isr_val = bm_ccsr_in(REG_ERR_ISR);
  143. ecsr_val = bm_ccsr_in(REG_ECSR);
  144. isr_mask = isr_val & ier_val;
  145. if (!isr_mask)
  146. return IRQ_NONE;
  147. for (i = 0; i < ARRAY_SIZE(bman_hwerr_txts); i++) {
  148. if (bman_hwerr_txts[i].mask & isr_mask) {
  149. dev_err_ratelimited(dev, "ErrInt: %s\n",
  150. bman_hwerr_txts[i].txt);
  151. if (bman_hwerr_txts[i].mask & ecsr_val) {
  152. /* Re-arm error capture registers */
  153. bm_ccsr_out(REG_ECSR, ecsr_val);
  154. }
  155. if (bman_hwerr_txts[i].mask & BMAN_ERRS_TO_DISABLE) {
  156. dev_dbg(dev, "Disabling error 0x%x\n",
  157. bman_hwerr_txts[i].mask);
  158. ier_val &= ~bman_hwerr_txts[i].mask;
  159. bm_ccsr_out(REG_ERR_IER, ier_val);
  160. }
  161. }
  162. }
  163. bm_ccsr_out(REG_ERR_ISR, isr_val);
  164. return IRQ_HANDLED;
  165. }
  166. int bman_is_probed(void)
  167. {
  168. return __bman_probed;
  169. }
  170. EXPORT_SYMBOL_GPL(bman_is_probed);
  171. int bman_requires_cleanup(void)
  172. {
  173. return __bman_requires_cleanup;
  174. }
  175. void bman_done_cleanup(void)
  176. {
  177. __bman_requires_cleanup = 0;
  178. }
  179. static int fsl_bman_probe(struct platform_device *pdev)
  180. {
  181. int ret, err_irq;
  182. struct device *dev = &pdev->dev;
  183. struct device_node *node = dev->of_node;
  184. struct resource *res;
  185. u16 id, bm_pool_cnt;
  186. u8 major, minor;
  187. __bman_probed = -1;
  188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. if (!res) {
  190. dev_err(dev, "Can't get %pOF property 'IORESOURCE_MEM'\n",
  191. node);
  192. return -ENXIO;
  193. }
  194. bm_ccsr_start = devm_ioremap(dev, res->start, resource_size(res));
  195. if (!bm_ccsr_start)
  196. return -ENXIO;
  197. bm_get_version(&id, &major, &minor);
  198. if (major == 1 && minor == 0) {
  199. bman_ip_rev = BMAN_REV10;
  200. bm_pool_cnt = BM_POOL_MAX;
  201. } else if (major == 2 && minor == 0) {
  202. bman_ip_rev = BMAN_REV20;
  203. bm_pool_cnt = 8;
  204. } else if (major == 2 && minor == 1) {
  205. bman_ip_rev = BMAN_REV21;
  206. bm_pool_cnt = BM_POOL_MAX;
  207. } else {
  208. dev_err(dev, "Unknown Bman version:%04x,%02x,%02x\n",
  209. id, major, minor);
  210. return -ENODEV;
  211. }
  212. /*
  213. * If FBPR memory wasn't defined using the qbman compatible string
  214. * try using the of_reserved_mem_device method
  215. */
  216. if (!fbpr_a) {
  217. ret = qbman_init_private_mem(dev, 0, &fbpr_a, &fbpr_sz);
  218. if (ret) {
  219. dev_err(dev, "qbman_init_private_mem() failed 0x%x\n",
  220. ret);
  221. return -ENODEV;
  222. }
  223. }
  224. dev_dbg(dev, "Allocated FBPR 0x%llx 0x%zx\n", fbpr_a, fbpr_sz);
  225. bm_set_memory(fbpr_a, fbpr_sz);
  226. err_irq = platform_get_irq(pdev, 0);
  227. if (err_irq <= 0) {
  228. dev_info(dev, "Can't get %pOF IRQ\n", node);
  229. return -ENODEV;
  230. }
  231. ret = devm_request_irq(dev, err_irq, bman_isr, IRQF_SHARED, "bman-err",
  232. dev);
  233. if (ret) {
  234. dev_err(dev, "devm_request_irq() failed %d for '%pOF'\n",
  235. ret, node);
  236. return ret;
  237. }
  238. /* Disable Buffer Pool State Change */
  239. bm_ccsr_out(REG_ERR_ISDR, BM_EIRQ_BSCN);
  240. /*
  241. * Write-to-clear any stale bits, (eg. starvation being asserted prior
  242. * to resource allocation during driver init).
  243. */
  244. bm_ccsr_out(REG_ERR_ISR, 0xffffffff);
  245. /* Enable Error Interrupts */
  246. bm_ccsr_out(REG_ERR_IER, 0xffffffff);
  247. bm_bpalloc = devm_gen_pool_create(dev, 0, -1, "bman-bpalloc");
  248. if (IS_ERR(bm_bpalloc)) {
  249. ret = PTR_ERR(bm_bpalloc);
  250. dev_err(dev, "bman-bpalloc pool init failed (%d)\n", ret);
  251. return ret;
  252. }
  253. /* seed BMan resource pool */
  254. ret = gen_pool_add(bm_bpalloc, DPAA_GENALLOC_OFF, bm_pool_cnt, -1);
  255. if (ret) {
  256. dev_err(dev, "Failed to seed BPID range [%d..%d] (%d)\n",
  257. 0, bm_pool_cnt - 1, ret);
  258. return ret;
  259. }
  260. __bman_probed = 1;
  261. return 0;
  262. };
  263. static const struct of_device_id fsl_bman_ids[] = {
  264. {
  265. .compatible = "fsl,bman",
  266. },
  267. {}
  268. };
  269. static struct platform_driver fsl_bman_driver = {
  270. .driver = {
  271. .name = KBUILD_MODNAME,
  272. .of_match_table = fsl_bman_ids,
  273. .suppress_bind_attrs = true,
  274. },
  275. .probe = fsl_bman_probe,
  276. };
  277. builtin_platform_driver(fsl_bman_driver);