zmii.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. dev->users++;
  82. return 0;
  83. }
  84. mutex_lock(&dev->lock);
  85. /* Autodetect ZMII mode if not specified.
  86. * This is only for backward compatibility with the old driver.
  87. * Please, always specify PHY mode in your board port to avoid
  88. * any surprises.
  89. */
  90. if (dev->mode == PHY_MODE_NA) {
  91. if (*mode == PHY_MODE_NA) {
  92. u32 r = dev->fer_save;
  93. ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
  94. if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
  95. dev->mode = PHY_MODE_MII;
  96. else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
  97. dev->mode = PHY_MODE_RMII;
  98. else
  99. dev->mode = PHY_MODE_SMII;
  100. } else
  101. dev->mode = *mode;
  102. printk(KERN_NOTICE "%s: bridge in %s mode\n",
  103. ofdev->node->full_name, zmii_mode_name(dev->mode));
  104. } else {
  105. /* All inputs must use the same mode */
  106. if (*mode != PHY_MODE_NA && *mode != dev->mode) {
  107. printk(KERN_ERR
  108. "%s: invalid mode %d specified for input %d\n",
  109. ofdev->node->full_name, *mode, input);
  110. mutex_unlock(&dev->lock);
  111. return -EINVAL;
  112. }
  113. }
  114. /* Report back correct PHY mode,
  115. * it may be used during PHY initialization.
  116. */
  117. *mode = dev->mode;
  118. /* Enable this input */
  119. out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
  120. ++dev->users;
  121. mutex_unlock(&dev->lock);
  122. return 0;
  123. }
  124. void zmii_get_mdio(struct of_device *ofdev, int input)
  125. {
  126. struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  127. u32 fer;
  128. ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
  129. mutex_lock(&dev->lock);
  130. fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
  131. out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
  132. }
  133. void zmii_put_mdio(struct of_device *ofdev, int input)
  134. {
  135. struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  136. ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
  137. mutex_unlock(&dev->lock);
  138. }
  139. void zmii_set_speed(struct of_device *ofdev, int input, int speed)
  140. {
  141. struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  142. u32 ssr;
  143. mutex_lock(&dev->lock);
  144. ssr = in_be32(&dev->base->ssr);
  145. ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
  146. if (speed == SPEED_100)
  147. ssr |= ZMII_SSR_SP(input);
  148. else
  149. ssr &= ~ZMII_SSR_SP(input);
  150. out_be32(&dev->base->ssr, ssr);
  151. mutex_unlock(&dev->lock);
  152. }
  153. void __devexit zmii_detach(struct of_device *ofdev, int input)
  154. {
  155. struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  156. BUG_ON(!dev || dev->users == 0);
  157. mutex_lock(&dev->lock);
  158. ZMII_DBG(dev, "detach(%d)" NL, input);
  159. /* Disable this input */
  160. out_be32(&dev->base->fer,
  161. in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
  162. --dev->users;
  163. mutex_unlock(&dev->lock);
  164. }
  165. int zmii_get_regs_len(struct of_device *ofdev)
  166. {
  167. return sizeof(struct emac_ethtool_regs_subhdr) +
  168. sizeof(struct zmii_regs);
  169. }
  170. void *zmii_dump_regs(struct of_device *ofdev, void *buf)
  171. {
  172. struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  173. struct emac_ethtool_regs_subhdr *hdr = buf;
  174. struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
  175. hdr->version = 0;
  176. hdr->index = 0; /* for now, are there chips with more than one
  177. * zmii ? if yes, then we'll add a cell_index
  178. * like we do for emac
  179. */
  180. memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
  181. return regs + 1;
  182. }
  183. static int __devinit zmii_probe(struct of_device *ofdev,
  184. const struct of_device_id *match)
  185. {
  186. struct device_node *np = ofdev->node;
  187. struct zmii_instance *dev;
  188. struct resource regs;
  189. int rc;
  190. rc = -ENOMEM;
  191. dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
  192. if (dev == NULL) {
  193. printk(KERN_ERR "%s: could not allocate ZMII device!\n",
  194. np->full_name);
  195. goto err_gone;
  196. }
  197. mutex_init(&dev->lock);
  198. dev->ofdev = ofdev;
  199. dev->mode = PHY_MODE_NA;
  200. rc = -ENXIO;
  201. if (of_address_to_resource(np, 0, &regs)) {
  202. printk(KERN_ERR "%s: Can't get registers address\n",
  203. np->full_name);
  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 "%s: Can't map device registers!\n",
  211. np->full_name);
  212. goto err_free;
  213. }
  214. /* We may need FER value for autodetection later */
  215. dev->fer_save = in_be32(&dev->base->fer);
  216. /* Disable all inputs by default */
  217. out_be32(&dev->base->fer, 0);
  218. printk(KERN_INFO
  219. "ZMII %s initialized\n", ofdev->node->full_name);
  220. wmb();
  221. dev_set_drvdata(&ofdev->dev, dev);
  222. return 0;
  223. err_free:
  224. kfree(dev);
  225. err_gone:
  226. return rc;
  227. }
  228. static int __devexit zmii_remove(struct of_device *ofdev)
  229. {
  230. struct zmii_instance *dev = dev_get_drvdata(&ofdev->dev);
  231. dev_set_drvdata(&ofdev->dev, NULL);
  232. WARN_ON(dev->users != 0);
  233. iounmap(dev->base);
  234. kfree(dev);
  235. return 0;
  236. }
  237. static struct of_device_id zmii_match[] =
  238. {
  239. {
  240. .compatible = "ibm,zmii",
  241. },
  242. /* For backward compat with old DT */
  243. {
  244. .type = "emac-zmii",
  245. },
  246. {},
  247. };
  248. static struct of_platform_driver zmii_driver = {
  249. .name = "emac-zmii",
  250. .match_table = zmii_match,
  251. .probe = zmii_probe,
  252. .remove = zmii_remove,
  253. };
  254. int __init zmii_init(void)
  255. {
  256. return of_register_platform_driver(&zmii_driver);
  257. }
  258. void zmii_exit(void)
  259. {
  260. of_unregister_platform_driver(&zmii_driver);
  261. }