zmii.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/ethernet/ibm/emac/zmii.c
  4. *
  5. * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
  6. *
  7. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  8. * <[email protected]>
  9. *
  10. * Based on the arch/ppc version of the driver:
  11. *
  12. * Copyright (c) 2004, 2005 Zultys Technologies.
  13. * Eugene Surovegin <[email protected]> or <[email protected]>
  14. *
  15. * Based on original work by
  16. * Armin Kuster <[email protected]>
  17. * Copyright 2001 MontaVista Softare Inc.
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/kernel.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/of_address.h>
  23. #include <asm/io.h>
  24. #include "emac.h"
  25. #include "core.h"
  26. /* ZMIIx_FER */
  27. #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
  28. #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
  29. ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
  30. #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
  31. #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
  32. #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
  33. /* ZMIIx_SSR */
  34. #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
  35. #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
  36. #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
  37. /* ZMII only supports MII, RMII and SMII
  38. * we also support autodetection for backward compatibility
  39. */
  40. static inline int zmii_valid_mode(int mode)
  41. {
  42. return mode == PHY_INTERFACE_MODE_MII ||
  43. mode == PHY_INTERFACE_MODE_RMII ||
  44. mode == PHY_INTERFACE_MODE_SMII ||
  45. mode == PHY_INTERFACE_MODE_NA;
  46. }
  47. static inline const char *zmii_mode_name(int mode)
  48. {
  49. switch (mode) {
  50. case PHY_INTERFACE_MODE_MII:
  51. return "MII";
  52. case PHY_INTERFACE_MODE_RMII:
  53. return "RMII";
  54. case PHY_INTERFACE_MODE_SMII:
  55. return "SMII";
  56. default:
  57. BUG();
  58. }
  59. }
  60. static inline u32 zmii_mode_mask(int mode, int input)
  61. {
  62. switch (mode) {
  63. case PHY_INTERFACE_MODE_MII:
  64. return ZMII_FER_MII(input);
  65. case PHY_INTERFACE_MODE_RMII:
  66. return ZMII_FER_RMII(input);
  67. case PHY_INTERFACE_MODE_SMII:
  68. return ZMII_FER_SMII(input);
  69. default:
  70. return 0;
  71. }
  72. }
  73. int zmii_attach(struct platform_device *ofdev, int input,
  74. phy_interface_t *mode)
  75. {
  76. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  77. struct zmii_regs __iomem *p = dev->base;
  78. ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
  79. if (!zmii_valid_mode(*mode)) {
  80. /* Probably an EMAC connected to RGMII,
  81. * but it still may need ZMII for MDIO so
  82. * we don't fail here.
  83. */
  84. dev->users++;
  85. return 0;
  86. }
  87. mutex_lock(&dev->lock);
  88. /* Autodetect ZMII mode if not specified.
  89. * This is only for backward compatibility with the old driver.
  90. * Please, always specify PHY mode in your board port to avoid
  91. * any surprises.
  92. */
  93. if (dev->mode == PHY_INTERFACE_MODE_NA) {
  94. if (*mode == PHY_INTERFACE_MODE_NA) {
  95. u32 r = dev->fer_save;
  96. ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
  97. if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
  98. dev->mode = PHY_INTERFACE_MODE_MII;
  99. else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
  100. dev->mode = PHY_INTERFACE_MODE_RMII;
  101. else
  102. dev->mode = PHY_INTERFACE_MODE_SMII;
  103. } else {
  104. dev->mode = *mode;
  105. }
  106. printk(KERN_NOTICE "%pOF: bridge in %s mode\n",
  107. ofdev->dev.of_node,
  108. zmii_mode_name(dev->mode));
  109. } else {
  110. /* All inputs must use the same mode */
  111. if (*mode != PHY_INTERFACE_MODE_NA && *mode != dev->mode) {
  112. printk(KERN_ERR
  113. "%pOF: invalid mode %d specified for input %d\n",
  114. ofdev->dev.of_node, *mode, input);
  115. mutex_unlock(&dev->lock);
  116. return -EINVAL;
  117. }
  118. }
  119. /* Report back correct PHY mode,
  120. * it may be used during PHY initialization.
  121. */
  122. *mode = dev->mode;
  123. /* Enable this input */
  124. out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
  125. ++dev->users;
  126. mutex_unlock(&dev->lock);
  127. return 0;
  128. }
  129. void zmii_get_mdio(struct platform_device *ofdev, int input)
  130. {
  131. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  132. u32 fer;
  133. ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
  134. mutex_lock(&dev->lock);
  135. fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
  136. out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
  137. }
  138. void zmii_put_mdio(struct platform_device *ofdev, int input)
  139. {
  140. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  141. ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
  142. mutex_unlock(&dev->lock);
  143. }
  144. void zmii_set_speed(struct platform_device *ofdev, int input, int speed)
  145. {
  146. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  147. u32 ssr;
  148. mutex_lock(&dev->lock);
  149. ssr = in_be32(&dev->base->ssr);
  150. ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
  151. if (speed == SPEED_100)
  152. ssr |= ZMII_SSR_SP(input);
  153. else
  154. ssr &= ~ZMII_SSR_SP(input);
  155. out_be32(&dev->base->ssr, ssr);
  156. mutex_unlock(&dev->lock);
  157. }
  158. void zmii_detach(struct platform_device *ofdev, int input)
  159. {
  160. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  161. BUG_ON(!dev || dev->users == 0);
  162. mutex_lock(&dev->lock);
  163. ZMII_DBG(dev, "detach(%d)" NL, input);
  164. /* Disable this input */
  165. out_be32(&dev->base->fer,
  166. in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
  167. --dev->users;
  168. mutex_unlock(&dev->lock);
  169. }
  170. int zmii_get_regs_len(struct platform_device *ofdev)
  171. {
  172. return sizeof(struct emac_ethtool_regs_subhdr) +
  173. sizeof(struct zmii_regs);
  174. }
  175. void *zmii_dump_regs(struct platform_device *ofdev, void *buf)
  176. {
  177. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  178. struct emac_ethtool_regs_subhdr *hdr = buf;
  179. struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
  180. hdr->version = 0;
  181. hdr->index = 0; /* for now, are there chips with more than one
  182. * zmii ? if yes, then we'll add a cell_index
  183. * like we do for emac
  184. */
  185. memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
  186. return regs + 1;
  187. }
  188. static int zmii_probe(struct platform_device *ofdev)
  189. {
  190. struct device_node *np = ofdev->dev.of_node;
  191. struct zmii_instance *dev;
  192. struct resource regs;
  193. int rc;
  194. rc = -ENOMEM;
  195. dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
  196. if (dev == NULL)
  197. goto err_gone;
  198. mutex_init(&dev->lock);
  199. dev->ofdev = ofdev;
  200. dev->mode = PHY_INTERFACE_MODE_NA;
  201. rc = -ENXIO;
  202. if (of_address_to_resource(np, 0, &regs)) {
  203. printk(KERN_ERR "%pOF: Can't get registers address\n", np);
  204. goto err_free;
  205. }
  206. rc = -ENOMEM;
  207. dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
  208. sizeof(struct zmii_regs));
  209. if (dev->base == NULL) {
  210. printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
  211. goto err_free;
  212. }
  213. /* We may need FER value for autodetection later */
  214. dev->fer_save = in_be32(&dev->base->fer);
  215. /* Disable all inputs by default */
  216. out_be32(&dev->base->fer, 0);
  217. printk(KERN_INFO "ZMII %pOF initialized\n", ofdev->dev.of_node);
  218. wmb();
  219. platform_set_drvdata(ofdev, dev);
  220. return 0;
  221. err_free:
  222. kfree(dev);
  223. err_gone:
  224. return rc;
  225. }
  226. static int zmii_remove(struct platform_device *ofdev)
  227. {
  228. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  229. WARN_ON(dev->users != 0);
  230. iounmap(dev->base);
  231. kfree(dev);
  232. return 0;
  233. }
  234. static const struct of_device_id zmii_match[] =
  235. {
  236. {
  237. .compatible = "ibm,zmii",
  238. },
  239. /* For backward compat with old DT */
  240. {
  241. .type = "emac-zmii",
  242. },
  243. {},
  244. };
  245. static struct platform_driver zmii_driver = {
  246. .driver = {
  247. .name = "emac-zmii",
  248. .of_match_table = zmii_match,
  249. },
  250. .probe = zmii_probe,
  251. .remove = zmii_remove,
  252. };
  253. int __init zmii_init(void)
  254. {
  255. return platform_driver_register(&zmii_driver);
  256. }
  257. void zmii_exit(void)
  258. {
  259. platform_driver_unregister(&zmii_driver);
  260. }