fec_mii.c 10 KB

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