zmii.c 7.4 KB

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