fec_mii.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  3. *
  4. * Copyright (c) 2003 Intracom S.A.
  5. * by Pantelis Antoniou <panto@intracom.gr>
  6. *
  7. * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
  8. * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
  9. *
  10. * Released under the GPL
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/errno.h>
  20. #include <linux/ioport.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/pci.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/bitops.h>
  33. #include <asm/8xx_immap.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/mpc8xx.h>
  36. #include <asm/irq.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/commproc.h>
  39. /*************************************************/
  40. #include "fec_8xx.h"
  41. /*************************************************/
  42. /* Make MII read/write commands for the FEC.
  43. */
  44. #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
  45. #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
  46. #define mk_mii_end 0
  47. /*************************************************/
  48. /* XXX both FECs use the MII interface of FEC1 */
  49. static DEFINE_SPINLOCK(fec_mii_lock);
  50. #define FEC_MII_LOOPS 10000
  51. int fec_mii_read(struct net_device *dev, int phy_id, int location)
  52. {
  53. struct fec_enet_private *fep = netdev_priv(dev);
  54. fec_t *fecp;
  55. int i, ret = -1;
  56. unsigned long flags;
  57. /* XXX MII interface is only connected to FEC1 */
  58. fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
  59. spin_lock_irqsave(&fec_mii_lock, flags);
  60. if ((FR(fecp, r_cntrl) & FEC_RCNTRL_MII_MODE) == 0) {
  61. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  62. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  63. FW(fecp, ievent, FEC_ENET_MII);
  64. }
  65. /* Add PHY address to register command. */
  66. FW(fecp, mii_speed, fep->fec_phy_speed);
  67. FW(fecp, mii_data, (phy_id << 23) | mk_mii_read(location));
  68. for (i = 0; i < FEC_MII_LOOPS; i++)
  69. if ((FR(fecp, ievent) & FEC_ENET_MII) != 0)
  70. break;
  71. if (i < FEC_MII_LOOPS) {
  72. FW(fecp, ievent, FEC_ENET_MII);
  73. ret = FR(fecp, mii_data) & 0xffff;
  74. }
  75. spin_unlock_irqrestore(&fec_mii_lock, flags);
  76. return ret;
  77. }
  78. void fec_mii_write(struct net_device *dev, int phy_id, int location, int value)
  79. {
  80. struct fec_enet_private *fep = netdev_priv(dev);
  81. fec_t *fecp;
  82. unsigned long flags;
  83. int i;
  84. /* XXX MII interface is only connected to FEC1 */
  85. fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
  86. spin_lock_irqsave(&fec_mii_lock, flags);
  87. if ((FR(fecp, r_cntrl) & FEC_RCNTRL_MII_MODE) == 0) {
  88. FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  89. FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  90. FW(fecp, ievent, FEC_ENET_MII);
  91. }
  92. /* Add PHY address to register command. */
  93. FW(fecp, mii_speed, fep->fec_phy_speed); /* always adapt mii speed */
  94. FW(fecp, mii_data, (phy_id << 23) | mk_mii_write(location, value));
  95. for (i = 0; i < FEC_MII_LOOPS; i++)
  96. if ((FR(fecp, ievent) & FEC_ENET_MII) != 0)
  97. break;
  98. if (i < FEC_MII_LOOPS)
  99. FW(fecp, ievent, FEC_ENET_MII);
  100. spin_unlock_irqrestore(&fec_mii_lock, flags);
  101. }
  102. /*************************************************/
  103. #ifdef CONFIG_FEC_8XX_GENERIC_PHY
  104. /*
  105. * Generic PHY support.
  106. * Should work for all PHYs, but link change is detected by polling
  107. */
  108. static void generic_timer_callback(unsigned long data)
  109. {
  110. struct net_device *dev = (struct net_device *)data;
  111. struct fec_enet_private *fep = netdev_priv(dev);
  112. fep->phy_timer_list.expires = jiffies + HZ / 2;
  113. add_timer(&fep->phy_timer_list);
  114. fec_mii_link_status_change_check(dev, 0);
  115. }
  116. static void generic_startup(struct net_device *dev)
  117. {
  118. struct fec_enet_private *fep = netdev_priv(dev);
  119. fep->phy_timer_list.expires = jiffies + HZ / 2; /* every 500ms */
  120. fep->phy_timer_list.data = (unsigned long)dev;
  121. fep->phy_timer_list.function = generic_timer_callback;
  122. add_timer(&fep->phy_timer_list);
  123. }
  124. static void generic_shutdown(struct net_device *dev)
  125. {
  126. struct fec_enet_private *fep = netdev_priv(dev);
  127. del_timer_sync(&fep->phy_timer_list);
  128. }
  129. #endif
  130. #ifdef CONFIG_FEC_8XX_DM9161_PHY
  131. /* ------------------------------------------------------------------------- */
  132. /* The Davicom DM9161 is used on the NETTA board */
  133. /* register definitions */
  134. #define MII_DM9161_ACR 16 /* Aux. Config Register */
  135. #define MII_DM9161_ACSR 17 /* Aux. Config/Status Register */
  136. #define MII_DM9161_10TCSR 18 /* 10BaseT Config/Status Reg. */
  137. #define MII_DM9161_INTR 21 /* Interrupt Register */
  138. #define MII_DM9161_RECR 22 /* Receive Error Counter Reg. */
  139. #define MII_DM9161_DISCR 23 /* Disconnect Counter Register */
  140. static void dm9161_startup(struct net_device *dev)
  141. {
  142. struct fec_enet_private *fep = netdev_priv(dev);
  143. fec_mii_write(dev, fep->mii_if.phy_id, MII_DM9161_INTR, 0x0000);
  144. }
  145. static void dm9161_ack_int(struct net_device *dev)
  146. {
  147. struct fec_enet_private *fep = netdev_priv(dev);
  148. fec_mii_read(dev, fep->mii_if.phy_id, MII_DM9161_INTR);
  149. }
  150. static void dm9161_shutdown(struct net_device *dev)
  151. {
  152. struct fec_enet_private *fep = netdev_priv(dev);
  153. fec_mii_write(dev, fep->mii_if.phy_id, MII_DM9161_INTR, 0x0f00);
  154. }
  155. #endif
  156. /**********************************************************************************/
  157. static const struct phy_info phy_info[] = {
  158. #ifdef CONFIG_FEC_8XX_DM9161_PHY
  159. {
  160. .id = 0x00181b88,
  161. .name = "DM9161",
  162. .startup = dm9161_startup,
  163. .ack_int = dm9161_ack_int,
  164. .shutdown = dm9161_shutdown,
  165. },
  166. #endif
  167. #ifdef CONFIG_FEC_8XX_GENERIC_PHY
  168. {
  169. .id = 0,
  170. .name = "GENERIC",
  171. .startup = generic_startup,
  172. .shutdown = generic_shutdown,
  173. },
  174. #endif
  175. };
  176. /**********************************************************************************/
  177. int fec_mii_phy_id_detect(struct net_device *dev)
  178. {
  179. struct fec_enet_private *fep = netdev_priv(dev);
  180. const struct fec_platform_info *fpi = fep->fpi;
  181. int i, r, start, end, phytype, physubtype;
  182. const struct phy_info *phy;
  183. int phy_hwid, phy_id;
  184. /* if no MDIO */
  185. if (fpi->use_mdio == 0)
  186. return -1;
  187. phy_hwid = -1;
  188. fep->phy = NULL;
  189. /* auto-detect? */
  190. if (fpi->phy_addr == -1) {
  191. start = 0;
  192. end = 32;
  193. } else { /* direct */
  194. start = fpi->phy_addr;
  195. end = start + 1;
  196. }
  197. for (phy_id = start; phy_id < end; phy_id++) {
  198. r = fec_mii_read(dev, phy_id, MII_PHYSID1);
  199. if (r == -1 || (phytype = (r & 0xffff)) == 0xffff)
  200. continue;
  201. r = fec_mii_read(dev, phy_id, MII_PHYSID2);
  202. if (r == -1 || (physubtype = (r & 0xffff)) == 0xffff)
  203. continue;
  204. phy_hwid = (phytype << 16) | physubtype;
  205. if (phy_hwid != -1)
  206. break;
  207. }
  208. if (phy_hwid == -1) {
  209. printk(KERN_ERR DRV_MODULE_NAME
  210. ": %s No PHY detected!\n", dev->name);
  211. return -1;
  212. }
  213. for (i = 0, phy = phy_info; i < sizeof(phy_info) / sizeof(phy_info[0]);
  214. i++, phy++)
  215. if (phy->id == (phy_hwid >> 4) || phy->id == 0)
  216. break;
  217. if (i >= sizeof(phy_info) / sizeof(phy_info[0])) {
  218. printk(KERN_ERR DRV_MODULE_NAME
  219. ": %s PHY id 0x%08x is not supported!\n",
  220. dev->name, phy_hwid);
  221. return -1;
  222. }
  223. fep->phy = phy;
  224. printk(KERN_INFO DRV_MODULE_NAME
  225. ": %s Phy @ 0x%x, type %s (0x%08x)\n",
  226. dev->name, phy_id, fep->phy->name, phy_hwid);
  227. return phy_id;
  228. }
  229. void fec_mii_startup(struct net_device *dev)
  230. {
  231. struct fec_enet_private *fep = netdev_priv(dev);
  232. const struct fec_platform_info *fpi = fep->fpi;
  233. if (!fpi->use_mdio || fep->phy == NULL)
  234. return;
  235. if (fep->phy->startup == NULL)
  236. return;
  237. (*fep->phy->startup) (dev);
  238. }
  239. void fec_mii_shutdown(struct net_device *dev)
  240. {
  241. struct fec_enet_private *fep = netdev_priv(dev);
  242. const struct fec_platform_info *fpi = fep->fpi;
  243. if (!fpi->use_mdio || fep->phy == NULL)
  244. return;
  245. if (fep->phy->shutdown == NULL)
  246. return;
  247. (*fep->phy->shutdown) (dev);
  248. }
  249. void fec_mii_ack_int(struct net_device *dev)
  250. {
  251. struct fec_enet_private *fep = netdev_priv(dev);
  252. const struct fec_platform_info *fpi = fep->fpi;
  253. if (!fpi->use_mdio || fep->phy == NULL)
  254. return;
  255. if (fep->phy->ack_int == NULL)
  256. return;
  257. (*fep->phy->ack_int) (dev);
  258. }
  259. /* helper function */
  260. static int mii_negotiated(struct mii_if_info *mii)
  261. {
  262. int advert, lpa, val;
  263. if (!mii_link_ok(mii))
  264. return 0;
  265. val = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_BMSR);
  266. if ((val & BMSR_ANEGCOMPLETE) == 0)
  267. return 0;
  268. advert = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_ADVERTISE);
  269. lpa = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_LPA);
  270. return mii_nway_result(advert & lpa);
  271. }
  272. void fec_mii_link_status_change_check(struct net_device *dev, int init_media)
  273. {
  274. struct fec_enet_private *fep = netdev_priv(dev);
  275. unsigned int media;
  276. unsigned long flags;
  277. if (mii_check_media(&fep->mii_if, netif_msg_link(fep), init_media) == 0)
  278. return;
  279. media = mii_negotiated(&fep->mii_if);
  280. if (netif_carrier_ok(dev)) {
  281. spin_lock_irqsave(&fep->lock, flags);
  282. fec_restart(dev, !!(media & ADVERTISE_FULL),
  283. (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)) ?
  284. 100 : 10);
  285. spin_unlock_irqrestore(&fep->lock, flags);
  286. netif_start_queue(dev);
  287. } else {
  288. netif_stop_queue(dev);
  289. spin_lock_irqsave(&fep->lock, flags);
  290. fec_stop(dev);
  291. spin_unlock_irqrestore(&fep->lock, flags);
  292. }
  293. }