brcmstb_gisb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  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 version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/io.h>
  20. #include <linux/string.h>
  21. #include <linux/device.h>
  22. #include <linux/list.h>
  23. #include <linux/of.h>
  24. #include <linux/bitops.h>
  25. #include <linux/pm.h>
  26. #ifdef CONFIG_ARM
  27. #include <asm/bug.h>
  28. #include <asm/signal.h>
  29. #endif
  30. #define ARB_ERR_CAP_CLEAR (1 << 0)
  31. #define ARB_ERR_CAP_STATUS_TIMEOUT (1 << 12)
  32. #define ARB_ERR_CAP_STATUS_TEA (1 << 11)
  33. #define ARB_ERR_CAP_STATUS_BS_SHIFT (1 << 2)
  34. #define ARB_ERR_CAP_STATUS_BS_MASK 0x3c
  35. #define ARB_ERR_CAP_STATUS_WRITE (1 << 1)
  36. #define ARB_ERR_CAP_STATUS_VALID (1 << 0)
  37. enum {
  38. ARB_TIMER,
  39. ARB_ERR_CAP_CLR,
  40. ARB_ERR_CAP_HI_ADDR,
  41. ARB_ERR_CAP_ADDR,
  42. ARB_ERR_CAP_DATA,
  43. ARB_ERR_CAP_STATUS,
  44. ARB_ERR_CAP_MASTER,
  45. };
  46. static const int gisb_offsets_bcm7038[] = {
  47. [ARB_TIMER] = 0x00c,
  48. [ARB_ERR_CAP_CLR] = 0x0c4,
  49. [ARB_ERR_CAP_HI_ADDR] = -1,
  50. [ARB_ERR_CAP_ADDR] = 0x0c8,
  51. [ARB_ERR_CAP_DATA] = 0x0cc,
  52. [ARB_ERR_CAP_STATUS] = 0x0d0,
  53. [ARB_ERR_CAP_MASTER] = -1,
  54. };
  55. static const int gisb_offsets_bcm7400[] = {
  56. [ARB_TIMER] = 0x00c,
  57. [ARB_ERR_CAP_CLR] = 0x0c8,
  58. [ARB_ERR_CAP_HI_ADDR] = -1,
  59. [ARB_ERR_CAP_ADDR] = 0x0cc,
  60. [ARB_ERR_CAP_DATA] = 0x0d0,
  61. [ARB_ERR_CAP_STATUS] = 0x0d4,
  62. [ARB_ERR_CAP_MASTER] = 0x0d8,
  63. };
  64. static const int gisb_offsets_bcm7435[] = {
  65. [ARB_TIMER] = 0x00c,
  66. [ARB_ERR_CAP_CLR] = 0x168,
  67. [ARB_ERR_CAP_HI_ADDR] = -1,
  68. [ARB_ERR_CAP_ADDR] = 0x16c,
  69. [ARB_ERR_CAP_DATA] = 0x170,
  70. [ARB_ERR_CAP_STATUS] = 0x174,
  71. [ARB_ERR_CAP_MASTER] = 0x178,
  72. };
  73. static const int gisb_offsets_bcm7445[] = {
  74. [ARB_TIMER] = 0x008,
  75. [ARB_ERR_CAP_CLR] = 0x7e4,
  76. [ARB_ERR_CAP_HI_ADDR] = 0x7e8,
  77. [ARB_ERR_CAP_ADDR] = 0x7ec,
  78. [ARB_ERR_CAP_DATA] = 0x7f0,
  79. [ARB_ERR_CAP_STATUS] = 0x7f4,
  80. [ARB_ERR_CAP_MASTER] = 0x7f8,
  81. };
  82. struct brcmstb_gisb_arb_device {
  83. void __iomem *base;
  84. const int *gisb_offsets;
  85. bool big_endian;
  86. struct mutex lock;
  87. struct list_head next;
  88. u32 valid_mask;
  89. const char *master_names[sizeof(u32) * BITS_PER_BYTE];
  90. u32 saved_timeout;
  91. };
  92. static LIST_HEAD(brcmstb_gisb_arb_device_list);
  93. static u32 gisb_read(struct brcmstb_gisb_arb_device *gdev, int reg)
  94. {
  95. int offset = gdev->gisb_offsets[reg];
  96. /* return 1 if the hardware doesn't have ARB_ERR_CAP_MASTER */
  97. if (offset == -1)
  98. return 1;
  99. if (gdev->big_endian)
  100. return ioread32be(gdev->base + offset);
  101. else
  102. return ioread32(gdev->base + offset);
  103. }
  104. static void gisb_write(struct brcmstb_gisb_arb_device *gdev, u32 val, int reg)
  105. {
  106. int offset = gdev->gisb_offsets[reg];
  107. if (offset == -1)
  108. return;
  109. if (gdev->big_endian)
  110. iowrite32be(val, gdev->base + reg);
  111. else
  112. iowrite32(val, gdev->base + reg);
  113. }
  114. static ssize_t gisb_arb_get_timeout(struct device *dev,
  115. struct device_attribute *attr,
  116. char *buf)
  117. {
  118. struct platform_device *pdev = to_platform_device(dev);
  119. struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
  120. u32 timeout;
  121. mutex_lock(&gdev->lock);
  122. timeout = gisb_read(gdev, ARB_TIMER);
  123. mutex_unlock(&gdev->lock);
  124. return sprintf(buf, "%d", timeout);
  125. }
  126. static ssize_t gisb_arb_set_timeout(struct device *dev,
  127. struct device_attribute *attr,
  128. const char *buf, size_t count)
  129. {
  130. struct platform_device *pdev = to_platform_device(dev);
  131. struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
  132. int val, ret;
  133. ret = kstrtoint(buf, 10, &val);
  134. if (ret < 0)
  135. return ret;
  136. if (val == 0 || val >= 0xffffffff)
  137. return -EINVAL;
  138. mutex_lock(&gdev->lock);
  139. gisb_write(gdev, val, ARB_TIMER);
  140. mutex_unlock(&gdev->lock);
  141. return count;
  142. }
  143. static const char *
  144. brcmstb_gisb_master_to_str(struct brcmstb_gisb_arb_device *gdev,
  145. u32 masters)
  146. {
  147. u32 mask = gdev->valid_mask & masters;
  148. if (hweight_long(mask) != 1)
  149. return NULL;
  150. return gdev->master_names[ffs(mask) - 1];
  151. }
  152. static int brcmstb_gisb_arb_decode_addr(struct brcmstb_gisb_arb_device *gdev,
  153. const char *reason)
  154. {
  155. u32 cap_status;
  156. unsigned long arb_addr;
  157. u32 master;
  158. const char *m_name;
  159. char m_fmt[11];
  160. cap_status = gisb_read(gdev, ARB_ERR_CAP_STATUS);
  161. /* Invalid captured address, bail out */
  162. if (!(cap_status & ARB_ERR_CAP_STATUS_VALID))
  163. return 1;
  164. /* Read the address and master */
  165. arb_addr = gisb_read(gdev, ARB_ERR_CAP_ADDR) & 0xffffffff;
  166. #if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT))
  167. arb_addr |= (u64)gisb_read(gdev, ARB_ERR_CAP_HI_ADDR) << 32;
  168. #endif
  169. master = gisb_read(gdev, ARB_ERR_CAP_MASTER);
  170. m_name = brcmstb_gisb_master_to_str(gdev, master);
  171. if (!m_name) {
  172. snprintf(m_fmt, sizeof(m_fmt), "0x%08x", master);
  173. m_name = m_fmt;
  174. }
  175. pr_crit("%s: %s at 0x%lx [%c %s], core: %s\n",
  176. __func__, reason, arb_addr,
  177. cap_status & ARB_ERR_CAP_STATUS_WRITE ? 'W' : 'R',
  178. cap_status & ARB_ERR_CAP_STATUS_TIMEOUT ? "timeout" : "",
  179. m_name);
  180. /* clear the GISB error */
  181. gisb_write(gdev, ARB_ERR_CAP_CLEAR, ARB_ERR_CAP_CLR);
  182. return 0;
  183. }
  184. #ifdef CONFIG_ARM
  185. static int brcmstb_bus_error_handler(unsigned long addr, unsigned int fsr,
  186. struct pt_regs *regs)
  187. {
  188. int ret = 0;
  189. struct brcmstb_gisb_arb_device *gdev;
  190. /* iterate over each GISB arb registered handlers */
  191. list_for_each_entry(gdev, &brcmstb_gisb_arb_device_list, next)
  192. ret |= brcmstb_gisb_arb_decode_addr(gdev, "bus error");
  193. /*
  194. * If it was an imprecise abort, then we need to correct the
  195. * return address to be _after_ the instruction.
  196. */
  197. if (fsr & (1 << 10))
  198. regs->ARM_pc += 4;
  199. return ret;
  200. }
  201. #endif
  202. static irqreturn_t brcmstb_gisb_timeout_handler(int irq, void *dev_id)
  203. {
  204. brcmstb_gisb_arb_decode_addr(dev_id, "timeout");
  205. return IRQ_HANDLED;
  206. }
  207. static irqreturn_t brcmstb_gisb_tea_handler(int irq, void *dev_id)
  208. {
  209. brcmstb_gisb_arb_decode_addr(dev_id, "target abort");
  210. return IRQ_HANDLED;
  211. }
  212. static DEVICE_ATTR(gisb_arb_timeout, S_IWUSR | S_IRUGO,
  213. gisb_arb_get_timeout, gisb_arb_set_timeout);
  214. static struct attribute *gisb_arb_sysfs_attrs[] = {
  215. &dev_attr_gisb_arb_timeout.attr,
  216. NULL,
  217. };
  218. static struct attribute_group gisb_arb_sysfs_attr_group = {
  219. .attrs = gisb_arb_sysfs_attrs,
  220. };
  221. static const struct of_device_id brcmstb_gisb_arb_of_match[] = {
  222. { .compatible = "brcm,gisb-arb", .data = gisb_offsets_bcm7445 },
  223. { .compatible = "brcm,bcm7445-gisb-arb", .data = gisb_offsets_bcm7445 },
  224. { .compatible = "brcm,bcm7435-gisb-arb", .data = gisb_offsets_bcm7435 },
  225. { .compatible = "brcm,bcm7400-gisb-arb", .data = gisb_offsets_bcm7400 },
  226. { .compatible = "brcm,bcm7038-gisb-arb", .data = gisb_offsets_bcm7038 },
  227. { },
  228. };
  229. static int __init brcmstb_gisb_arb_probe(struct platform_device *pdev)
  230. {
  231. struct device_node *dn = pdev->dev.of_node;
  232. struct brcmstb_gisb_arb_device *gdev;
  233. const struct of_device_id *of_id;
  234. struct resource *r;
  235. int err, timeout_irq, tea_irq;
  236. unsigned int num_masters, j = 0;
  237. int i, first, last;
  238. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  239. timeout_irq = platform_get_irq(pdev, 0);
  240. tea_irq = platform_get_irq(pdev, 1);
  241. gdev = devm_kzalloc(&pdev->dev, sizeof(*gdev), GFP_KERNEL);
  242. if (!gdev)
  243. return -ENOMEM;
  244. mutex_init(&gdev->lock);
  245. INIT_LIST_HEAD(&gdev->next);
  246. gdev->base = devm_ioremap_resource(&pdev->dev, r);
  247. if (IS_ERR(gdev->base))
  248. return PTR_ERR(gdev->base);
  249. of_id = of_match_node(brcmstb_gisb_arb_of_match, dn);
  250. if (!of_id) {
  251. pr_err("failed to look up compatible string\n");
  252. return -EINVAL;
  253. }
  254. gdev->gisb_offsets = of_id->data;
  255. gdev->big_endian = of_device_is_big_endian(dn);
  256. err = devm_request_irq(&pdev->dev, timeout_irq,
  257. brcmstb_gisb_timeout_handler, 0, pdev->name,
  258. gdev);
  259. if (err < 0)
  260. return err;
  261. err = devm_request_irq(&pdev->dev, tea_irq,
  262. brcmstb_gisb_tea_handler, 0, pdev->name,
  263. gdev);
  264. if (err < 0)
  265. return err;
  266. /* If we do not have a valid mask, assume all masters are enabled */
  267. if (of_property_read_u32(dn, "brcm,gisb-arb-master-mask",
  268. &gdev->valid_mask))
  269. gdev->valid_mask = 0xffffffff;
  270. /* Proceed with reading the litteral names if we agree on the
  271. * number of masters
  272. */
  273. num_masters = of_property_count_strings(dn,
  274. "brcm,gisb-arb-master-names");
  275. if (hweight_long(gdev->valid_mask) == num_masters) {
  276. first = ffs(gdev->valid_mask) - 1;
  277. last = fls(gdev->valid_mask) - 1;
  278. for (i = first; i < last; i++) {
  279. if (!(gdev->valid_mask & BIT(i)))
  280. continue;
  281. of_property_read_string_index(dn,
  282. "brcm,gisb-arb-master-names", j,
  283. &gdev->master_names[i]);
  284. j++;
  285. }
  286. }
  287. err = sysfs_create_group(&pdev->dev.kobj, &gisb_arb_sysfs_attr_group);
  288. if (err)
  289. return err;
  290. platform_set_drvdata(pdev, gdev);
  291. list_add_tail(&gdev->next, &brcmstb_gisb_arb_device_list);
  292. #ifdef CONFIG_ARM
  293. hook_fault_code(22, brcmstb_bus_error_handler, SIGBUS, 0,
  294. "imprecise external abort");
  295. #endif
  296. dev_info(&pdev->dev, "registered mem: %p, irqs: %d, %d\n",
  297. gdev->base, timeout_irq, tea_irq);
  298. return 0;
  299. }
  300. #ifdef CONFIG_PM_SLEEP
  301. static int brcmstb_gisb_arb_suspend(struct device *dev)
  302. {
  303. struct platform_device *pdev = to_platform_device(dev);
  304. struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
  305. gdev->saved_timeout = gisb_read(gdev, ARB_TIMER);
  306. return 0;
  307. }
  308. /* Make sure we provide the same timeout value that was configured before, and
  309. * do this before the GISB timeout interrupt handler has any chance to run.
  310. */
  311. static int brcmstb_gisb_arb_resume_noirq(struct device *dev)
  312. {
  313. struct platform_device *pdev = to_platform_device(dev);
  314. struct brcmstb_gisb_arb_device *gdev = platform_get_drvdata(pdev);
  315. gisb_write(gdev, gdev->saved_timeout, ARB_TIMER);
  316. return 0;
  317. }
  318. #else
  319. #define brcmstb_gisb_arb_suspend NULL
  320. #define brcmstb_gisb_arb_resume_noirq NULL
  321. #endif
  322. static const struct dev_pm_ops brcmstb_gisb_arb_pm_ops = {
  323. .suspend = brcmstb_gisb_arb_suspend,
  324. .resume_noirq = brcmstb_gisb_arb_resume_noirq,
  325. };
  326. static struct platform_driver brcmstb_gisb_arb_driver = {
  327. .driver = {
  328. .name = "brcm-gisb-arb",
  329. .of_match_table = brcmstb_gisb_arb_of_match,
  330. .pm = &brcmstb_gisb_arb_pm_ops,
  331. },
  332. };
  333. static int __init brcm_gisb_driver_init(void)
  334. {
  335. return platform_driver_probe(&brcmstb_gisb_arb_driver,
  336. brcmstb_gisb_arb_probe);
  337. }
  338. module_init(brcm_gisb_driver_init);