b53_mmap.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * B53 register access through memory mapped registers
  3. *
  4. * Copyright (C) 2012-2013 Jonas Gorski <jogo@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/platform_data/b53.h>
  22. #include "b53_priv.h"
  23. static int b53_mmap_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
  24. {
  25. u8 __iomem *regs = dev->priv;
  26. *val = readb(regs + (page << 8) + reg);
  27. return 0;
  28. }
  29. static int b53_mmap_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
  30. {
  31. u8 __iomem *regs = dev->priv;
  32. if (WARN_ON(reg % 2))
  33. return -EINVAL;
  34. if (dev->pdata && dev->pdata->big_endian)
  35. *val = readw_be(regs + (page << 8) + reg);
  36. else
  37. *val = readw(regs + (page << 8) + reg);
  38. return 0;
  39. }
  40. static int b53_mmap_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
  41. {
  42. u8 __iomem *regs = dev->priv;
  43. if (WARN_ON(reg % 4))
  44. return -EINVAL;
  45. if (dev->pdata && dev->pdata->big_endian)
  46. *val = readl_be(regs + (page << 8) + reg);
  47. else
  48. *val = readl(regs + (page << 8) + reg);
  49. return 0;
  50. }
  51. static int b53_mmap_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  52. {
  53. if (WARN_ON(reg % 2))
  54. return -EINVAL;
  55. if (reg % 4) {
  56. u16 lo;
  57. u32 hi;
  58. b53_mmap_read16(dev, page, reg, &lo);
  59. b53_mmap_read32(dev, page, reg + 2, &hi);
  60. *val = ((u64)hi << 16) | lo;
  61. } else {
  62. u32 lo;
  63. u16 hi;
  64. b53_mmap_read32(dev, page, reg, &lo);
  65. b53_mmap_read16(dev, page, reg + 4, &hi);
  66. *val = ((u64)hi << 32) | lo;
  67. }
  68. return 0;
  69. }
  70. static int b53_mmap_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
  71. {
  72. u32 hi, lo;
  73. if (WARN_ON(reg % 4))
  74. return -EINVAL;
  75. b53_mmap_read32(dev, page, reg, &lo);
  76. b53_mmap_read32(dev, page, reg + 4, &hi);
  77. *val = ((u64)hi << 32) | lo;
  78. return 0;
  79. }
  80. static int b53_mmap_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
  81. {
  82. u8 __iomem *regs = dev->priv;
  83. writeb(value, regs + (page << 8) + reg);
  84. return 0;
  85. }
  86. static int b53_mmap_write16(struct b53_device *dev, u8 page, u8 reg,
  87. u16 value)
  88. {
  89. u8 __iomem *regs = dev->priv;
  90. if (WARN_ON(reg % 2))
  91. return -EINVAL;
  92. if (dev->pdata && dev->pdata->big_endian)
  93. writew_be(value, regs + (page << 8) + reg);
  94. else
  95. writew(value, regs + (page << 8) + reg);
  96. return 0;
  97. }
  98. static int b53_mmap_write32(struct b53_device *dev, u8 page, u8 reg,
  99. u32 value)
  100. {
  101. u8 __iomem *regs = dev->priv;
  102. if (WARN_ON(reg % 4))
  103. return -EINVAL;
  104. if (dev->pdata && dev->pdata->big_endian)
  105. writel_be(value, regs + (page << 8) + reg);
  106. else
  107. writel(value, regs + (page << 8) + reg);
  108. return 0;
  109. }
  110. static int b53_mmap_write48(struct b53_device *dev, u8 page, u8 reg,
  111. u64 value)
  112. {
  113. if (WARN_ON(reg % 2))
  114. return -EINVAL;
  115. if (reg % 4) {
  116. u32 hi = (u32)(value >> 16);
  117. u16 lo = (u16)value;
  118. b53_mmap_write16(dev, page, reg, lo);
  119. b53_mmap_write32(dev, page, reg + 2, hi);
  120. } else {
  121. u16 hi = (u16)(value >> 32);
  122. u32 lo = (u32)value;
  123. b53_mmap_write32(dev, page, reg, lo);
  124. b53_mmap_write16(dev, page, reg + 4, hi);
  125. }
  126. return 0;
  127. }
  128. static int b53_mmap_write64(struct b53_device *dev, u8 page, u8 reg,
  129. u64 value)
  130. {
  131. u32 hi, lo;
  132. hi = (u32)(value >> 32);
  133. lo = (u32)value;
  134. if (WARN_ON(reg % 4))
  135. return -EINVAL;
  136. b53_mmap_write32(dev, page, reg, lo);
  137. b53_mmap_write32(dev, page, reg + 4, hi);
  138. return 0;
  139. }
  140. static struct b53_io_ops b53_mmap_ops = {
  141. .read8 = b53_mmap_read8,
  142. .read16 = b53_mmap_read16,
  143. .read32 = b53_mmap_read32,
  144. .read48 = b53_mmap_read48,
  145. .read64 = b53_mmap_read64,
  146. .write8 = b53_mmap_write8,
  147. .write16 = b53_mmap_write16,
  148. .write32 = b53_mmap_write32,
  149. .write48 = b53_mmap_write48,
  150. .write64 = b53_mmap_write64,
  151. };
  152. static int b53_mmap_probe(struct platform_device *pdev)
  153. {
  154. struct b53_platform_data *pdata = pdev->dev.platform_data;
  155. struct b53_device *dev;
  156. if (!pdata)
  157. return -EINVAL;
  158. dev = b53_switch_alloc(&pdev->dev, &b53_mmap_ops, pdata->regs);
  159. if (!dev)
  160. return -ENOMEM;
  161. if (pdata)
  162. dev->pdata = pdata;
  163. platform_set_drvdata(pdev, dev);
  164. return b53_switch_register(dev);
  165. }
  166. static int b53_mmap_remove(struct platform_device *pdev)
  167. {
  168. struct b53_device *dev = platform_get_drvdata(pdev);
  169. if (dev)
  170. b53_switch_remove(dev);
  171. return 0;
  172. }
  173. static struct platform_driver b53_mmap_driver = {
  174. .probe = b53_mmap_probe,
  175. .remove = b53_mmap_remove,
  176. .driver = {
  177. .name = "b53-switch",
  178. },
  179. };
  180. module_platform_driver(b53_mmap_driver);
  181. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  182. MODULE_DESCRIPTION("B53 MMAP access driver");
  183. MODULE_LICENSE("Dual BSD/GPL");