zmii.c 7.6 KB

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