rgmii.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * drivers/net/ibm_newemac/rgmii.c
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
  5. *
  6. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  7. * <benh@kernel.crashing.org>
  8. *
  9. * Based on the arch/ppc version of the driver:
  10. *
  11. * Copyright (c) 2004, 2005 Zultys Technologies.
  12. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  13. *
  14. * Based on original work by
  15. * Matt Porter <mporter@kernel.crashing.org>
  16. * Copyright 2004 MontaVista Software, Inc.
  17. *
  18. * This program is free software; you can redistribute it and/or modify it
  19. * under the terms of the GNU General Public License as published by the
  20. * Free Software Foundation; either version 2 of the License, or (at your
  21. * option) any later version.
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/ethtool.h>
  26. #include <asm/io.h>
  27. #include "emac.h"
  28. #include "debug.h"
  29. // XXX FIXME: Axon seems to support a subset of the RGMII, we
  30. // thus need to take that into account and possibly change some
  31. // of the bit settings below that don't seem to quite match the
  32. // AXON spec
  33. /* RGMIIx_FER */
  34. #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
  35. #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
  36. #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
  37. #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
  38. #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
  39. /* RGMIIx_SSR */
  40. #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
  41. #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
  42. #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
  43. /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
  44. static inline int rgmii_valid_mode(int phy_mode)
  45. {
  46. return phy_mode == PHY_MODE_GMII ||
  47. phy_mode == PHY_MODE_RGMII ||
  48. phy_mode == PHY_MODE_TBI ||
  49. phy_mode == PHY_MODE_RTBI;
  50. }
  51. static inline const char *rgmii_mode_name(int mode)
  52. {
  53. switch (mode) {
  54. case PHY_MODE_RGMII:
  55. return "RGMII";
  56. case PHY_MODE_TBI:
  57. return "TBI";
  58. case PHY_MODE_GMII:
  59. return "GMII";
  60. case PHY_MODE_RTBI:
  61. return "RTBI";
  62. default:
  63. BUG();
  64. }
  65. }
  66. static inline u32 rgmii_mode_mask(int mode, int input)
  67. {
  68. switch (mode) {
  69. case PHY_MODE_RGMII:
  70. return RGMII_FER_RGMII(input);
  71. case PHY_MODE_TBI:
  72. return RGMII_FER_TBI(input);
  73. case PHY_MODE_GMII:
  74. return RGMII_FER_GMII(input);
  75. case PHY_MODE_RTBI:
  76. return RGMII_FER_RTBI(input);
  77. default:
  78. BUG();
  79. }
  80. }
  81. int __devinit rgmii_attach(struct of_device *ofdev, int input, int mode)
  82. {
  83. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  84. struct rgmii_regs __iomem *p = dev->base;
  85. RGMII_DBG(dev, "attach(%d)" NL, input);
  86. /* Check if we need to attach to a RGMII */
  87. if (input < 0 || !rgmii_valid_mode(mode)) {
  88. printk(KERN_ERR "%s: unsupported settings !\n",
  89. ofdev->node->full_name);
  90. return -ENODEV;
  91. }
  92. mutex_lock(&dev->lock);
  93. /* Enable this input */
  94. out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
  95. printk(KERN_NOTICE "%s: input %d in %s mode\n",
  96. ofdev->node->full_name, input, rgmii_mode_name(mode));
  97. ++dev->users;
  98. mutex_unlock(&dev->lock);
  99. return 0;
  100. }
  101. void rgmii_set_speed(struct of_device *ofdev, int input, int speed)
  102. {
  103. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  104. struct rgmii_regs __iomem *p = dev->base;
  105. u32 ssr;
  106. mutex_lock(&dev->lock);
  107. ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
  108. RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
  109. if (speed == SPEED_1000)
  110. ssr |= RGMII_SSR_1000(input);
  111. else if (speed == SPEED_100)
  112. ssr |= RGMII_SSR_100(input);
  113. out_be32(&p->ssr, ssr);
  114. mutex_unlock(&dev->lock);
  115. }
  116. void rgmii_get_mdio(struct of_device *ofdev, int input)
  117. {
  118. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  119. struct rgmii_regs __iomem *p = dev->base;
  120. u32 fer;
  121. RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
  122. if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
  123. return;
  124. mutex_lock(&dev->lock);
  125. fer = in_be32(&p->fer);
  126. fer |= 0x00080000u >> input;
  127. out_be32(&p->fer, fer);
  128. (void)in_be32(&p->fer);
  129. DBG2(dev, " fer = 0x%08x\n", fer);
  130. }
  131. void rgmii_put_mdio(struct of_device *ofdev, int input)
  132. {
  133. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  134. struct rgmii_regs __iomem *p = dev->base;
  135. u32 fer;
  136. RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
  137. if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
  138. return;
  139. fer = in_be32(&p->fer);
  140. fer &= ~(0x00080000u >> input);
  141. out_be32(&p->fer, fer);
  142. (void)in_be32(&p->fer);
  143. DBG2(dev, " fer = 0x%08x\n", fer);
  144. mutex_unlock(&dev->lock);
  145. }
  146. void __devexit rgmii_detach(struct of_device *ofdev, int input)
  147. {
  148. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  149. struct rgmii_regs __iomem *p = dev->base;
  150. mutex_lock(&dev->lock);
  151. BUG_ON(!dev || dev->users == 0);
  152. RGMII_DBG(dev, "detach(%d)" NL, input);
  153. /* Disable this input */
  154. out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
  155. --dev->users;
  156. mutex_unlock(&dev->lock);
  157. }
  158. int rgmii_get_regs_len(struct of_device *ofdev)
  159. {
  160. return sizeof(struct emac_ethtool_regs_subhdr) +
  161. sizeof(struct rgmii_regs);
  162. }
  163. void *rgmii_dump_regs(struct of_device *ofdev, void *buf)
  164. {
  165. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  166. struct emac_ethtool_regs_subhdr *hdr = buf;
  167. struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
  168. hdr->version = 0;
  169. hdr->index = 0; /* for now, are there chips with more than one
  170. * rgmii ? if yes, then we'll add a cell_index
  171. * like we do for emac
  172. */
  173. memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
  174. return regs + 1;
  175. }
  176. static int __devinit rgmii_probe(struct of_device *ofdev,
  177. const struct of_device_id *match)
  178. {
  179. struct device_node *np = ofdev->node;
  180. struct rgmii_instance *dev;
  181. struct resource regs;
  182. int rc;
  183. rc = -ENOMEM;
  184. dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
  185. if (dev == NULL) {
  186. printk(KERN_ERR "%s: could not allocate RGMII device!\n",
  187. np->full_name);
  188. goto err_gone;
  189. }
  190. mutex_init(&dev->lock);
  191. dev->ofdev = ofdev;
  192. rc = -ENXIO;
  193. if (of_address_to_resource(np, 0, &regs)) {
  194. printk(KERN_ERR "%s: Can't get registers address\n",
  195. np->full_name);
  196. goto err_free;
  197. }
  198. rc = -ENOMEM;
  199. dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
  200. sizeof(struct rgmii_regs));
  201. if (dev->base == NULL) {
  202. printk(KERN_ERR "%s: Can't map device registers!\n",
  203. np->full_name);
  204. goto err_free;
  205. }
  206. /* Check for RGMII flags */
  207. if (of_get_property(ofdev->node, "has-mdio", NULL))
  208. dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
  209. /* CAB lacks the right properties, fix this up */
  210. if (of_device_is_compatible(ofdev->node, "ibm,rgmii-axon"))
  211. dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
  212. DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
  213. in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
  214. /* Disable all inputs by default */
  215. out_be32(&dev->base->fer, 0);
  216. printk(KERN_INFO
  217. "RGMII %s initialized with%s MDIO support\n",
  218. ofdev->node->full_name,
  219. (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
  220. wmb();
  221. dev_set_drvdata(&ofdev->dev, dev);
  222. return 0;
  223. err_free:
  224. kfree(dev);
  225. err_gone:
  226. return rc;
  227. }
  228. static int __devexit rgmii_remove(struct of_device *ofdev)
  229. {
  230. struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  231. dev_set_drvdata(&ofdev->dev, NULL);
  232. WARN_ON(dev->users != 0);
  233. iounmap(dev->base);
  234. kfree(dev);
  235. return 0;
  236. }
  237. static struct of_device_id rgmii_match[] =
  238. {
  239. {
  240. .type = "rgmii-interface",
  241. .compatible = "ibm,rgmii",
  242. },
  243. {
  244. .type = "emac-rgmii",
  245. },
  246. {},
  247. };
  248. static struct of_platform_driver rgmii_driver = {
  249. .name = "emac-rgmii",
  250. .match_table = rgmii_match,
  251. .probe = rgmii_probe,
  252. .remove = rgmii_remove,
  253. };
  254. int __init rgmii_init(void)
  255. {
  256. return of_register_platform_driver(&rgmii_driver);
  257. }
  258. void rgmii_exit(void)
  259. {
  260. of_unregister_platform_driver(&rgmii_driver);
  261. }