zmii.c 7.5 KB

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