zmii.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_MODE_MII ||
  48. mode == PHY_MODE_RMII ||
  49. mode == PHY_MODE_SMII ||
  50. mode == PHY_MODE_NA;
  51. }
  52. static inline const char *zmii_mode_name(int mode)
  53. {
  54. switch (mode) {
  55. case PHY_MODE_MII:
  56. return "MII";
  57. case PHY_MODE_RMII:
  58. return "RMII";
  59. case PHY_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_MODE_MII:
  69. return ZMII_FER_MII(input);
  70. case PHY_MODE_RMII:
  71. return ZMII_FER_RMII(input);
  72. case PHY_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_MODE_NA) {
  98. if (*mode == PHY_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_MODE_MII;
  103. else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
  104. dev->mode = PHY_MODE_RMII;
  105. else
  106. dev->mode = PHY_MODE_SMII;
  107. } else
  108. dev->mode = *mode;
  109. printk(KERN_NOTICE "%s: bridge in %s mode\n",
  110. ofdev->dev.of_node->full_name,
  111. zmii_mode_name(dev->mode));
  112. } else {
  113. /* All inputs must use the same mode */
  114. if (*mode != PHY_MODE_NA && *mode != dev->mode) {
  115. printk(KERN_ERR
  116. "%s: invalid mode %d specified for input %d\n",
  117. ofdev->dev.of_node->full_name, *mode, input);
  118. mutex_unlock(&dev->lock);
  119. return -EINVAL;
  120. }
  121. }
  122. /* Report back correct PHY mode,
  123. * it may be used during PHY initialization.
  124. */
  125. *mode = dev->mode;
  126. /* Enable this input */
  127. out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
  128. ++dev->users;
  129. mutex_unlock(&dev->lock);
  130. return 0;
  131. }
  132. void zmii_get_mdio(struct platform_device *ofdev, int input)
  133. {
  134. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  135. u32 fer;
  136. ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
  137. mutex_lock(&dev->lock);
  138. fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
  139. out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
  140. }
  141. void zmii_put_mdio(struct platform_device *ofdev, int input)
  142. {
  143. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  144. ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
  145. mutex_unlock(&dev->lock);
  146. }
  147. void zmii_set_speed(struct platform_device *ofdev, int input, int speed)
  148. {
  149. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  150. u32 ssr;
  151. mutex_lock(&dev->lock);
  152. ssr = in_be32(&dev->base->ssr);
  153. ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
  154. if (speed == SPEED_100)
  155. ssr |= ZMII_SSR_SP(input);
  156. else
  157. ssr &= ~ZMII_SSR_SP(input);
  158. out_be32(&dev->base->ssr, ssr);
  159. mutex_unlock(&dev->lock);
  160. }
  161. void zmii_detach(struct platform_device *ofdev, int input)
  162. {
  163. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  164. BUG_ON(!dev || dev->users == 0);
  165. mutex_lock(&dev->lock);
  166. ZMII_DBG(dev, "detach(%d)" NL, input);
  167. /* Disable this input */
  168. out_be32(&dev->base->fer,
  169. in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
  170. --dev->users;
  171. mutex_unlock(&dev->lock);
  172. }
  173. int zmii_get_regs_len(struct platform_device *ofdev)
  174. {
  175. return sizeof(struct emac_ethtool_regs_subhdr) +
  176. sizeof(struct zmii_regs);
  177. }
  178. void *zmii_dump_regs(struct platform_device *ofdev, void *buf)
  179. {
  180. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  181. struct emac_ethtool_regs_subhdr *hdr = buf;
  182. struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
  183. hdr->version = 0;
  184. hdr->index = 0; /* for now, are there chips with more than one
  185. * zmii ? if yes, then we'll add a cell_index
  186. * like we do for emac
  187. */
  188. memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
  189. return regs + 1;
  190. }
  191. static int zmii_probe(struct platform_device *ofdev)
  192. {
  193. struct device_node *np = ofdev->dev.of_node;
  194. struct zmii_instance *dev;
  195. struct resource regs;
  196. int rc;
  197. rc = -ENOMEM;
  198. dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
  199. if (dev == NULL)
  200. goto err_gone;
  201. mutex_init(&dev->lock);
  202. dev->ofdev = ofdev;
  203. dev->mode = PHY_MODE_NA;
  204. rc = -ENXIO;
  205. if (of_address_to_resource(np, 0, &regs)) {
  206. printk(KERN_ERR "%s: Can't get registers address\n",
  207. np->full_name);
  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 "%s: Can't map device registers!\n",
  215. np->full_name);
  216. goto err_free;
  217. }
  218. /* We may need FER value for autodetection later */
  219. dev->fer_save = in_be32(&dev->base->fer);
  220. /* Disable all inputs by default */
  221. out_be32(&dev->base->fer, 0);
  222. printk(KERN_INFO
  223. "ZMII %s initialized\n", ofdev->dev.of_node->full_name);
  224. wmb();
  225. platform_set_drvdata(ofdev, dev);
  226. return 0;
  227. err_free:
  228. kfree(dev);
  229. err_gone:
  230. return rc;
  231. }
  232. static int zmii_remove(struct platform_device *ofdev)
  233. {
  234. struct zmii_instance *dev = platform_get_drvdata(ofdev);
  235. WARN_ON(dev->users != 0);
  236. iounmap(dev->base);
  237. kfree(dev);
  238. return 0;
  239. }
  240. static const struct of_device_id zmii_match[] =
  241. {
  242. {
  243. .compatible = "ibm,zmii",
  244. },
  245. /* For backward compat with old DT */
  246. {
  247. .type = "emac-zmii",
  248. },
  249. {},
  250. };
  251. static struct platform_driver zmii_driver = {
  252. .driver = {
  253. .name = "emac-zmii",
  254. .of_match_table = zmii_match,
  255. },
  256. .probe = zmii_probe,
  257. .remove = zmii_remove,
  258. };
  259. int __init zmii_init(void)
  260. {
  261. return platform_driver_register(&zmii_driver);
  262. }
  263. void zmii_exit(void)
  264. {
  265. platform_driver_unregister(&zmii_driver);
  266. }