host_soc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Broadcom specific AMBA
  3. * System on Chip (SoC) Host
  4. *
  5. * Licensed under the GNU/GPL. See COPYING for details.
  6. */
  7. #include "bcma_private.h"
  8. #include "scan.h"
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/of_address.h>
  12. #include <linux/bcma/bcma.h>
  13. #include <linux/bcma/bcma_soc.h>
  14. static u8 bcma_host_soc_read8(struct bcma_device *core, u16 offset)
  15. {
  16. return readb(core->io_addr + offset);
  17. }
  18. static u16 bcma_host_soc_read16(struct bcma_device *core, u16 offset)
  19. {
  20. return readw(core->io_addr + offset);
  21. }
  22. static u32 bcma_host_soc_read32(struct bcma_device *core, u16 offset)
  23. {
  24. return readl(core->io_addr + offset);
  25. }
  26. static void bcma_host_soc_write8(struct bcma_device *core, u16 offset,
  27. u8 value)
  28. {
  29. writeb(value, core->io_addr + offset);
  30. }
  31. static void bcma_host_soc_write16(struct bcma_device *core, u16 offset,
  32. u16 value)
  33. {
  34. writew(value, core->io_addr + offset);
  35. }
  36. static void bcma_host_soc_write32(struct bcma_device *core, u16 offset,
  37. u32 value)
  38. {
  39. writel(value, core->io_addr + offset);
  40. }
  41. #ifdef CONFIG_BCMA_BLOCKIO
  42. static void bcma_host_soc_block_read(struct bcma_device *core, void *buffer,
  43. size_t count, u16 offset, u8 reg_width)
  44. {
  45. void __iomem *addr = core->io_addr + offset;
  46. switch (reg_width) {
  47. case sizeof(u8): {
  48. u8 *buf = buffer;
  49. while (count) {
  50. *buf = __raw_readb(addr);
  51. buf++;
  52. count--;
  53. }
  54. break;
  55. }
  56. case sizeof(u16): {
  57. __le16 *buf = buffer;
  58. WARN_ON(count & 1);
  59. while (count) {
  60. *buf = (__force __le16)__raw_readw(addr);
  61. buf++;
  62. count -= 2;
  63. }
  64. break;
  65. }
  66. case sizeof(u32): {
  67. __le32 *buf = buffer;
  68. WARN_ON(count & 3);
  69. while (count) {
  70. *buf = (__force __le32)__raw_readl(addr);
  71. buf++;
  72. count -= 4;
  73. }
  74. break;
  75. }
  76. default:
  77. WARN_ON(1);
  78. }
  79. }
  80. static void bcma_host_soc_block_write(struct bcma_device *core,
  81. const void *buffer,
  82. size_t count, u16 offset, u8 reg_width)
  83. {
  84. void __iomem *addr = core->io_addr + offset;
  85. switch (reg_width) {
  86. case sizeof(u8): {
  87. const u8 *buf = buffer;
  88. while (count) {
  89. __raw_writeb(*buf, addr);
  90. buf++;
  91. count--;
  92. }
  93. break;
  94. }
  95. case sizeof(u16): {
  96. const __le16 *buf = buffer;
  97. WARN_ON(count & 1);
  98. while (count) {
  99. __raw_writew((__force u16)(*buf), addr);
  100. buf++;
  101. count -= 2;
  102. }
  103. break;
  104. }
  105. case sizeof(u32): {
  106. const __le32 *buf = buffer;
  107. WARN_ON(count & 3);
  108. while (count) {
  109. __raw_writel((__force u32)(*buf), addr);
  110. buf++;
  111. count -= 4;
  112. }
  113. break;
  114. }
  115. default:
  116. WARN_ON(1);
  117. }
  118. }
  119. #endif /* CONFIG_BCMA_BLOCKIO */
  120. static u32 bcma_host_soc_aread32(struct bcma_device *core, u16 offset)
  121. {
  122. if (WARN_ONCE(!core->io_wrap, "Accessed core has no wrapper/agent\n"))
  123. return ~0;
  124. return readl(core->io_wrap + offset);
  125. }
  126. static void bcma_host_soc_awrite32(struct bcma_device *core, u16 offset,
  127. u32 value)
  128. {
  129. if (WARN_ONCE(!core->io_wrap, "Accessed core has no wrapper/agent\n"))
  130. return;
  131. writel(value, core->io_wrap + offset);
  132. }
  133. static const struct bcma_host_ops bcma_host_soc_ops = {
  134. .read8 = bcma_host_soc_read8,
  135. .read16 = bcma_host_soc_read16,
  136. .read32 = bcma_host_soc_read32,
  137. .write8 = bcma_host_soc_write8,
  138. .write16 = bcma_host_soc_write16,
  139. .write32 = bcma_host_soc_write32,
  140. #ifdef CONFIG_BCMA_BLOCKIO
  141. .block_read = bcma_host_soc_block_read,
  142. .block_write = bcma_host_soc_block_write,
  143. #endif
  144. .aread32 = bcma_host_soc_aread32,
  145. .awrite32 = bcma_host_soc_awrite32,
  146. };
  147. int __init bcma_host_soc_register(struct bcma_soc *soc)
  148. {
  149. struct bcma_bus *bus = &soc->bus;
  150. /* iomap only first core. We have to read some register on this core
  151. * to scan the bus.
  152. */
  153. bus->mmio = ioremap_nocache(BCMA_ADDR_BASE, BCMA_CORE_SIZE * 1);
  154. if (!bus->mmio)
  155. return -ENOMEM;
  156. /* Host specific */
  157. bus->hosttype = BCMA_HOSTTYPE_SOC;
  158. bus->ops = &bcma_host_soc_ops;
  159. bus->host_pdev = NULL;
  160. /* Initialize struct, detect chip */
  161. bcma_init_bus(bus);
  162. return 0;
  163. }
  164. int __init bcma_host_soc_init(struct bcma_soc *soc)
  165. {
  166. struct bcma_bus *bus = &soc->bus;
  167. int err;
  168. /* Scan bus and initialize it */
  169. err = bcma_bus_early_register(bus);
  170. if (err)
  171. iounmap(bus->mmio);
  172. return err;
  173. }
  174. #ifdef CONFIG_OF
  175. static int bcma_host_soc_probe(struct platform_device *pdev)
  176. {
  177. struct device *dev = &pdev->dev;
  178. struct device_node *np = dev->of_node;
  179. struct bcma_bus *bus;
  180. int err;
  181. /* Alloc */
  182. bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
  183. if (!bus)
  184. return -ENOMEM;
  185. /* Map MMIO */
  186. bus->mmio = of_iomap(np, 0);
  187. if (!bus->mmio)
  188. return -ENOMEM;
  189. /* Host specific */
  190. bus->hosttype = BCMA_HOSTTYPE_SOC;
  191. bus->ops = &bcma_host_soc_ops;
  192. bus->host_pdev = pdev;
  193. /* Initialize struct, detect chip */
  194. bcma_init_bus(bus);
  195. /* Register */
  196. err = bcma_bus_register(bus);
  197. if (err)
  198. goto err_unmap_mmio;
  199. platform_set_drvdata(pdev, bus);
  200. return err;
  201. err_unmap_mmio:
  202. iounmap(bus->mmio);
  203. return err;
  204. }
  205. static int bcma_host_soc_remove(struct platform_device *pdev)
  206. {
  207. struct bcma_bus *bus = platform_get_drvdata(pdev);
  208. bcma_bus_unregister(bus);
  209. iounmap(bus->mmio);
  210. platform_set_drvdata(pdev, NULL);
  211. return 0;
  212. }
  213. static const struct of_device_id bcma_host_soc_of_match[] = {
  214. { .compatible = "brcm,bus-axi", },
  215. {},
  216. };
  217. MODULE_DEVICE_TABLE(of, bcma_host_soc_of_match);
  218. static struct platform_driver bcma_host_soc_driver = {
  219. .driver = {
  220. .name = "bcma-host-soc",
  221. .of_match_table = bcma_host_soc_of_match,
  222. },
  223. .probe = bcma_host_soc_probe,
  224. .remove = bcma_host_soc_remove,
  225. };
  226. int __init bcma_host_soc_register_driver(void)
  227. {
  228. return platform_driver_register(&bcma_host_soc_driver);
  229. }
  230. void __exit bcma_host_soc_unregister_driver(void)
  231. {
  232. platform_driver_unregister(&bcma_host_soc_driver);
  233. }
  234. #endif /* CONFIG_OF */