host_soc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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(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. /* Initialize struct, detect chip */
  160. bcma_init_bus(bus);
  161. return 0;
  162. }
  163. int __init bcma_host_soc_init(struct bcma_soc *soc)
  164. {
  165. struct bcma_bus *bus = &soc->bus;
  166. int err;
  167. /* Scan bus and initialize it */
  168. err = bcma_bus_early_register(bus);
  169. if (err)
  170. iounmap(bus->mmio);
  171. return err;
  172. }
  173. #ifdef CONFIG_OF
  174. static int bcma_host_soc_probe(struct platform_device *pdev)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct device_node *np = dev->of_node;
  178. struct bcma_bus *bus;
  179. int err;
  180. /* Alloc */
  181. bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
  182. if (!bus)
  183. return -ENOMEM;
  184. bus->dev = dev;
  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. /* Initialize struct, detect chip */
  193. bcma_init_bus(bus);
  194. /* Register */
  195. err = bcma_bus_register(bus);
  196. if (err)
  197. goto err_unmap_mmio;
  198. platform_set_drvdata(pdev, bus);
  199. return err;
  200. err_unmap_mmio:
  201. iounmap(bus->mmio);
  202. return err;
  203. }
  204. static int bcma_host_soc_remove(struct platform_device *pdev)
  205. {
  206. struct bcma_bus *bus = platform_get_drvdata(pdev);
  207. bcma_bus_unregister(bus);
  208. iounmap(bus->mmio);
  209. platform_set_drvdata(pdev, NULL);
  210. return 0;
  211. }
  212. static const struct of_device_id bcma_host_soc_of_match[] = {
  213. { .compatible = "brcm,bus-axi", },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(of, bcma_host_soc_of_match);
  217. static struct platform_driver bcma_host_soc_driver = {
  218. .driver = {
  219. .name = "bcma-host-soc",
  220. .of_match_table = bcma_host_soc_of_match,
  221. },
  222. .probe = bcma_host_soc_probe,
  223. .remove = bcma_host_soc_remove,
  224. };
  225. int __init bcma_host_soc_register_driver(void)
  226. {
  227. return platform_driver_register(&bcma_host_soc_driver);
  228. }
  229. void __exit bcma_host_soc_unregister_driver(void)
  230. {
  231. platform_driver_unregister(&bcma_host_soc_driver);
  232. }
  233. #endif /* CONFIG_OF */