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