zmii.c 7.3 KB

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