ucc_geth_phy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright (C) Freescale Semicondutor, Inc. 2006. All rights reserved.
  3. *
  4. * Author: Shlomi Gridish <gridish@freescale.com>
  5. *
  6. * Description:
  7. * UCC GETH Driver -- PHY handling
  8. *
  9. * Changelog:
  10. * Jun 28, 2006 Li Yang <LeoLi@freescale.com>
  11. * - Rearrange code and style fixes
  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/sched.h>
  21. #include <linux/string.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/version.h>
  34. #include <linux/crc32.h>
  35. #include <linux/mii.h>
  36. #include <linux/ethtool.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #include <asm/uaccess.h>
  40. #include "ucc_geth.h"
  41. #include "ucc_geth_phy.h"
  42. #define ugphy_printk(level, format, arg...) \
  43. printk(level format "\n", ## arg)
  44. #define ugphy_dbg(format, arg...) \
  45. ugphy_printk(KERN_DEBUG, format , ## arg)
  46. #define ugphy_err(format, arg...) \
  47. ugphy_printk(KERN_ERR, format , ## arg)
  48. #define ugphy_info(format, arg...) \
  49. ugphy_printk(KERN_INFO, format , ## arg)
  50. #define ugphy_warn(format, arg...) \
  51. ugphy_printk(KERN_WARNING, format , ## arg)
  52. #ifdef UGETH_VERBOSE_DEBUG
  53. #define ugphy_vdbg ugphy_dbg
  54. #else
  55. #define ugphy_vdbg(fmt, args...) do { } while (0)
  56. #endif /* UGETH_VERBOSE_DEBUG */
  57. static void config_genmii_advert(struct ugeth_mii_info *mii_info);
  58. static void genmii_setup_forced(struct ugeth_mii_info *mii_info);
  59. static void genmii_restart_aneg(struct ugeth_mii_info *mii_info);
  60. static int gbit_config_aneg(struct ugeth_mii_info *mii_info);
  61. static int genmii_config_aneg(struct ugeth_mii_info *mii_info);
  62. static int genmii_update_link(struct ugeth_mii_info *mii_info);
  63. static int genmii_read_status(struct ugeth_mii_info *mii_info);
  64. u16 phy_read(struct ugeth_mii_info *mii_info, u16 regnum);
  65. void phy_write(struct ugeth_mii_info *mii_info, u16 regnum, u16 val);
  66. /* Write value to the PHY for this device to the register at regnum, */
  67. /* waiting until the write is done before it returns. All PHY */
  68. /* configuration has to be done through the TSEC1 MIIM regs */
  69. void write_phy_reg(struct net_device *dev, int mii_id, int regnum, int value)
  70. {
  71. struct ucc_geth_private *ugeth = netdev_priv(dev);
  72. struct ucc_mii_mng *mii_regs;
  73. enum enet_tbi_mii_reg mii_reg = (enum enet_tbi_mii_reg) regnum;
  74. u32 tmp_reg;
  75. ugphy_vdbg("%s: IN", __FUNCTION__);
  76. spin_lock_irq(&ugeth->lock);
  77. mii_regs = ugeth->mii_info->mii_regs;
  78. /* Set this UCC to be the master of the MII managment */
  79. ucc_set_qe_mux_mii_mng(ugeth->ug_info->uf_info.ucc_num);
  80. /* Stop the MII management read cycle */
  81. out_be32(&mii_regs->miimcom, 0);
  82. /* Setting up the MII Mangement Address Register */
  83. tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
  84. out_be32(&mii_regs->miimadd, tmp_reg);
  85. /* Setting up the MII Mangement Control Register with the value */
  86. out_be32(&mii_regs->miimcon, (u32) value);
  87. /* Wait till MII management write is complete */
  88. while ((in_be32(&mii_regs->miimind)) & MIIMIND_BUSY)
  89. cpu_relax();
  90. spin_unlock_irq(&ugeth->lock);
  91. udelay(10000);
  92. }
  93. /* Reads from register regnum in the PHY for device dev, */
  94. /* returning the value. Clears miimcom first. All PHY */
  95. /* configuration has to be done through the TSEC1 MIIM regs */
  96. int read_phy_reg(struct net_device *dev, int mii_id, int regnum)
  97. {
  98. struct ucc_geth_private *ugeth = netdev_priv(dev);
  99. struct ucc_mii_mng *mii_regs;
  100. enum enet_tbi_mii_reg mii_reg = (enum enet_tbi_mii_reg) regnum;
  101. u32 tmp_reg;
  102. u16 value;
  103. ugphy_vdbg("%s: IN", __FUNCTION__);
  104. spin_lock_irq(&ugeth->lock);
  105. mii_regs = ugeth->mii_info->mii_regs;
  106. /* Setting up the MII Mangement Address Register */
  107. tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
  108. out_be32(&mii_regs->miimadd, tmp_reg);
  109. /* Perform an MII management read cycle */
  110. out_be32(&mii_regs->miimcom, MIIMCOM_READ_CYCLE);
  111. /* Wait till MII management write is complete */
  112. while ((in_be32(&mii_regs->miimind)) & MIIMIND_BUSY)
  113. cpu_relax();
  114. udelay(10000);
  115. /* Read MII management status */
  116. value = (u16) in_be32(&mii_regs->miimstat);
  117. out_be32(&mii_regs->miimcom, 0);
  118. if (value == 0xffff)
  119. ugphy_warn("read wrong value : mii_id %d,mii_reg %d, base %08x",
  120. mii_id, mii_reg, (u32) & (mii_regs->miimcfg));
  121. spin_unlock_irq(&ugeth->lock);
  122. return (value);
  123. }
  124. void mii_clear_phy_interrupt(struct ugeth_mii_info *mii_info)
  125. {
  126. ugphy_vdbg("%s: IN", __FUNCTION__);
  127. if (mii_info->phyinfo->ack_interrupt)
  128. mii_info->phyinfo->ack_interrupt(mii_info);
  129. }
  130. void mii_configure_phy_interrupt(struct ugeth_mii_info *mii_info,
  131. u32 interrupts)
  132. {
  133. ugphy_vdbg("%s: IN", __FUNCTION__);
  134. mii_info->interrupts = interrupts;
  135. if (mii_info->phyinfo->config_intr)
  136. mii_info->phyinfo->config_intr(mii_info);
  137. }
  138. /* Writes MII_ADVERTISE with the appropriate values, after
  139. * sanitizing advertise to make sure only supported features
  140. * are advertised
  141. */
  142. static void config_genmii_advert(struct ugeth_mii_info *mii_info)
  143. {
  144. u32 advertise;
  145. u16 adv;
  146. ugphy_vdbg("%s: IN", __FUNCTION__);
  147. /* Only allow advertising what this PHY supports */
  148. mii_info->advertising &= mii_info->phyinfo->features;
  149. advertise = mii_info->advertising;
  150. /* Setup standard advertisement */
  151. adv = phy_read(mii_info, MII_ADVERTISE);
  152. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  153. if (advertise & ADVERTISED_10baseT_Half)
  154. adv |= ADVERTISE_10HALF;
  155. if (advertise & ADVERTISED_10baseT_Full)
  156. adv |= ADVERTISE_10FULL;
  157. if (advertise & ADVERTISED_100baseT_Half)
  158. adv |= ADVERTISE_100HALF;
  159. if (advertise & ADVERTISED_100baseT_Full)
  160. adv |= ADVERTISE_100FULL;
  161. phy_write(mii_info, MII_ADVERTISE, adv);
  162. }
  163. static void genmii_setup_forced(struct ugeth_mii_info *mii_info)
  164. {
  165. u16 ctrl;
  166. u32 features = mii_info->phyinfo->features;
  167. ugphy_vdbg("%s: IN", __FUNCTION__);
  168. ctrl = phy_read(mii_info, MII_BMCR);
  169. ctrl &=
  170. ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_ANENABLE);
  171. ctrl |= BMCR_RESET;
  172. switch (mii_info->speed) {
  173. case SPEED_1000:
  174. if (features & (SUPPORTED_1000baseT_Half
  175. | SUPPORTED_1000baseT_Full)) {
  176. ctrl |= BMCR_SPEED1000;
  177. break;
  178. }
  179. mii_info->speed = SPEED_100;
  180. case SPEED_100:
  181. if (features & (SUPPORTED_100baseT_Half
  182. | SUPPORTED_100baseT_Full)) {
  183. ctrl |= BMCR_SPEED100;
  184. break;
  185. }
  186. mii_info->speed = SPEED_10;
  187. case SPEED_10:
  188. if (features & (SUPPORTED_10baseT_Half
  189. | SUPPORTED_10baseT_Full))
  190. break;
  191. default: /* Unsupported speed! */
  192. ugphy_err("%s: Bad speed!", mii_info->dev->name);
  193. break;
  194. }
  195. phy_write(mii_info, MII_BMCR, ctrl);
  196. }
  197. /* Enable and Restart Autonegotiation */
  198. static void genmii_restart_aneg(struct ugeth_mii_info *mii_info)
  199. {
  200. u16 ctl;
  201. ugphy_vdbg("%s: IN", __FUNCTION__);
  202. ctl = phy_read(mii_info, MII_BMCR);
  203. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  204. phy_write(mii_info, MII_BMCR, ctl);
  205. }
  206. static int gbit_config_aneg(struct ugeth_mii_info *mii_info)
  207. {
  208. u16 adv;
  209. u32 advertise;
  210. ugphy_vdbg("%s: IN", __FUNCTION__);
  211. if (mii_info->autoneg) {
  212. /* Configure the ADVERTISE register */
  213. config_genmii_advert(mii_info);
  214. advertise = mii_info->advertising;
  215. adv = phy_read(mii_info, MII_1000BASETCONTROL);
  216. adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
  217. MII_1000BASETCONTROL_HALFDUPLEXCAP);
  218. if (advertise & SUPPORTED_1000baseT_Half)
  219. adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
  220. if (advertise & SUPPORTED_1000baseT_Full)
  221. adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
  222. phy_write(mii_info, MII_1000BASETCONTROL, adv);
  223. /* Start/Restart aneg */
  224. genmii_restart_aneg(mii_info);
  225. } else
  226. genmii_setup_forced(mii_info);
  227. return 0;
  228. }
  229. static int genmii_config_aneg(struct ugeth_mii_info *mii_info)
  230. {
  231. ugphy_vdbg("%s: IN", __FUNCTION__);
  232. if (mii_info->autoneg) {
  233. config_genmii_advert(mii_info);
  234. genmii_restart_aneg(mii_info);
  235. } else
  236. genmii_setup_forced(mii_info);
  237. return 0;
  238. }
  239. static int genmii_update_link(struct ugeth_mii_info *mii_info)
  240. {
  241. u16 status;
  242. ugphy_vdbg("%s: IN", __FUNCTION__);
  243. /* Do a fake read */
  244. phy_read(mii_info, MII_BMSR);
  245. /* Read link and autonegotiation status */
  246. status = phy_read(mii_info, MII_BMSR);
  247. if ((status & BMSR_LSTATUS) == 0)
  248. mii_info->link = 0;
  249. else
  250. mii_info->link = 1;
  251. /* If we are autonegotiating, and not done,
  252. * return an error */
  253. if (mii_info->autoneg && !(status & BMSR_ANEGCOMPLETE))
  254. return -EAGAIN;
  255. return 0;
  256. }
  257. static int genmii_read_status(struct ugeth_mii_info *mii_info)
  258. {
  259. u16 status;
  260. int err;
  261. ugphy_vdbg("%s: IN", __FUNCTION__);
  262. /* Update the link, but return if there
  263. * was an error */
  264. err = genmii_update_link(mii_info);
  265. if (err)
  266. return err;
  267. if (mii_info->autoneg) {
  268. status = phy_read(mii_info, MII_LPA);
  269. if (status & (LPA_10FULL | LPA_100FULL))
  270. mii_info->duplex = DUPLEX_FULL;
  271. else
  272. mii_info->duplex = DUPLEX_HALF;
  273. if (status & (LPA_100FULL | LPA_100HALF))
  274. mii_info->speed = SPEED_100;
  275. else
  276. mii_info->speed = SPEED_10;
  277. mii_info->pause = 0;
  278. }
  279. /* On non-aneg, we assume what we put in BMCR is the speed,
  280. * though magic-aneg shouldn't prevent this case from occurring
  281. */
  282. return 0;
  283. }
  284. static int marvell_init(struct ugeth_mii_info *mii_info)
  285. {
  286. ugphy_vdbg("%s: IN", __FUNCTION__);
  287. phy_write(mii_info, 0x14, 0x0cd2);
  288. phy_write(mii_info, MII_BMCR,
  289. phy_read(mii_info, MII_BMCR) | BMCR_RESET);
  290. msleep(4000);
  291. return 0;
  292. }
  293. static int marvell_config_aneg(struct ugeth_mii_info *mii_info)
  294. {
  295. ugphy_vdbg("%s: IN", __FUNCTION__);
  296. /* The Marvell PHY has an errata which requires
  297. * that certain registers get written in order
  298. * to restart autonegotiation */
  299. phy_write(mii_info, MII_BMCR, BMCR_RESET);
  300. phy_write(mii_info, 0x1d, 0x1f);
  301. phy_write(mii_info, 0x1e, 0x200c);
  302. phy_write(mii_info, 0x1d, 0x5);
  303. phy_write(mii_info, 0x1e, 0);
  304. phy_write(mii_info, 0x1e, 0x100);
  305. gbit_config_aneg(mii_info);
  306. return 0;
  307. }
  308. static int marvell_read_status(struct ugeth_mii_info *mii_info)
  309. {
  310. u16 status;
  311. int err;
  312. ugphy_vdbg("%s: IN", __FUNCTION__);
  313. /* Update the link, but return if there
  314. * was an error */
  315. err = genmii_update_link(mii_info);
  316. if (err)
  317. return err;
  318. /* If the link is up, read the speed and duplex */
  319. /* If we aren't autonegotiating, assume speeds
  320. * are as set */
  321. if (mii_info->autoneg && mii_info->link) {
  322. int speed;
  323. status = phy_read(mii_info, MII_M1011_PHY_SPEC_STATUS);
  324. /* Get the duplexity */
  325. if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
  326. mii_info->duplex = DUPLEX_FULL;
  327. else
  328. mii_info->duplex = DUPLEX_HALF;
  329. /* Get the speed */
  330. speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
  331. switch (speed) {
  332. case MII_M1011_PHY_SPEC_STATUS_1000:
  333. mii_info->speed = SPEED_1000;
  334. break;
  335. case MII_M1011_PHY_SPEC_STATUS_100:
  336. mii_info->speed = SPEED_100;
  337. break;
  338. default:
  339. mii_info->speed = SPEED_10;
  340. break;
  341. }
  342. mii_info->pause = 0;
  343. }
  344. return 0;
  345. }
  346. static int marvell_ack_interrupt(struct ugeth_mii_info *mii_info)
  347. {
  348. ugphy_vdbg("%s: IN", __FUNCTION__);
  349. /* Clear the interrupts by reading the reg */
  350. phy_read(mii_info, MII_M1011_IEVENT);
  351. return 0;
  352. }
  353. static int marvell_config_intr(struct ugeth_mii_info *mii_info)
  354. {
  355. ugphy_vdbg("%s: IN", __FUNCTION__);
  356. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  357. phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
  358. else
  359. phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
  360. return 0;
  361. }
  362. static int cis820x_init(struct ugeth_mii_info *mii_info)
  363. {
  364. ugphy_vdbg("%s: IN", __FUNCTION__);
  365. phy_write(mii_info, MII_CIS8201_AUX_CONSTAT,
  366. MII_CIS8201_AUXCONSTAT_INIT);
  367. phy_write(mii_info, MII_CIS8201_EXT_CON1, MII_CIS8201_EXTCON1_INIT);
  368. return 0;
  369. }
  370. static int cis820x_read_status(struct ugeth_mii_info *mii_info)
  371. {
  372. u16 status;
  373. int err;
  374. ugphy_vdbg("%s: IN", __FUNCTION__);
  375. /* Update the link, but return if there
  376. * was an error */
  377. err = genmii_update_link(mii_info);
  378. if (err)
  379. return err;
  380. /* If the link is up, read the speed and duplex */
  381. /* If we aren't autonegotiating, assume speeds
  382. * are as set */
  383. if (mii_info->autoneg && mii_info->link) {
  384. int speed;
  385. status = phy_read(mii_info, MII_CIS8201_AUX_CONSTAT);
  386. if (status & MII_CIS8201_AUXCONSTAT_DUPLEX)
  387. mii_info->duplex = DUPLEX_FULL;
  388. else
  389. mii_info->duplex = DUPLEX_HALF;
  390. speed = status & MII_CIS8201_AUXCONSTAT_SPEED;
  391. switch (speed) {
  392. case MII_CIS8201_AUXCONSTAT_GBIT:
  393. mii_info->speed = SPEED_1000;
  394. break;
  395. case MII_CIS8201_AUXCONSTAT_100:
  396. mii_info->speed = SPEED_100;
  397. break;
  398. default:
  399. mii_info->speed = SPEED_10;
  400. break;
  401. }
  402. }
  403. return 0;
  404. }
  405. static int cis820x_ack_interrupt(struct ugeth_mii_info *mii_info)
  406. {
  407. ugphy_vdbg("%s: IN", __FUNCTION__);
  408. phy_read(mii_info, MII_CIS8201_ISTAT);
  409. return 0;
  410. }
  411. static int cis820x_config_intr(struct ugeth_mii_info *mii_info)
  412. {
  413. ugphy_vdbg("%s: IN", __FUNCTION__);
  414. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  415. phy_write(mii_info, MII_CIS8201_IMASK, MII_CIS8201_IMASK_MASK);
  416. else
  417. phy_write(mii_info, MII_CIS8201_IMASK, 0);
  418. return 0;
  419. }
  420. #define DM9161_DELAY 10
  421. static int dm9161_read_status(struct ugeth_mii_info *mii_info)
  422. {
  423. u16 status;
  424. int err;
  425. ugphy_vdbg("%s: IN", __FUNCTION__);
  426. /* Update the link, but return if there
  427. * was an error */
  428. err = genmii_update_link(mii_info);
  429. if (err)
  430. return err;
  431. /* If the link is up, read the speed and duplex */
  432. /* If we aren't autonegotiating, assume speeds
  433. * are as set */
  434. if (mii_info->autoneg && mii_info->link) {
  435. status = phy_read(mii_info, MII_DM9161_SCSR);
  436. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
  437. mii_info->speed = SPEED_100;
  438. else
  439. mii_info->speed = SPEED_10;
  440. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
  441. mii_info->duplex = DUPLEX_FULL;
  442. else
  443. mii_info->duplex = DUPLEX_HALF;
  444. }
  445. return 0;
  446. }
  447. static int dm9161_config_aneg(struct ugeth_mii_info *mii_info)
  448. {
  449. struct dm9161_private *priv = mii_info->priv;
  450. ugphy_vdbg("%s: IN", __FUNCTION__);
  451. if (0 == priv->resetdone)
  452. return -EAGAIN;
  453. return 0;
  454. }
  455. static void dm9161_timer(unsigned long data)
  456. {
  457. struct ugeth_mii_info *mii_info = (struct ugeth_mii_info *)data;
  458. struct dm9161_private *priv = mii_info->priv;
  459. u16 status = phy_read(mii_info, MII_BMSR);
  460. ugphy_vdbg("%s: IN", __FUNCTION__);
  461. if (status & BMSR_ANEGCOMPLETE) {
  462. priv->resetdone = 1;
  463. } else
  464. mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
  465. }
  466. static int dm9161_init(struct ugeth_mii_info *mii_info)
  467. {
  468. struct dm9161_private *priv;
  469. ugphy_vdbg("%s: IN", __FUNCTION__);
  470. /* Allocate the private data structure */
  471. priv = kmalloc(sizeof(struct dm9161_private), GFP_KERNEL);
  472. if (NULL == priv)
  473. return -ENOMEM;
  474. mii_info->priv = priv;
  475. /* Reset is not done yet */
  476. priv->resetdone = 0;
  477. phy_write(mii_info, MII_BMCR,
  478. phy_read(mii_info, MII_BMCR) | BMCR_RESET);
  479. phy_write(mii_info, MII_BMCR,
  480. phy_read(mii_info, MII_BMCR) & ~BMCR_ISOLATE);
  481. config_genmii_advert(mii_info);
  482. /* Start/Restart aneg */
  483. genmii_config_aneg(mii_info);
  484. /* Start a timer for DM9161_DELAY seconds to wait
  485. * for the PHY to be ready */
  486. init_timer(&priv->timer);
  487. priv->timer.function = &dm9161_timer;
  488. priv->timer.data = (unsigned long)mii_info;
  489. mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
  490. return 0;
  491. }
  492. static void dm9161_close(struct ugeth_mii_info *mii_info)
  493. {
  494. struct dm9161_private *priv = mii_info->priv;
  495. ugphy_vdbg("%s: IN", __FUNCTION__);
  496. del_timer_sync(&priv->timer);
  497. kfree(priv);
  498. }
  499. static int dm9161_ack_interrupt(struct ugeth_mii_info *mii_info)
  500. {
  501. ugphy_vdbg("%s: IN", __FUNCTION__);
  502. /* Clear the interrupts by reading the reg */
  503. phy_read(mii_info, MII_DM9161_INTR);
  504. return 0;
  505. }
  506. static int dm9161_config_intr(struct ugeth_mii_info *mii_info)
  507. {
  508. ugphy_vdbg("%s: IN", __FUNCTION__);
  509. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  510. phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
  511. else
  512. phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
  513. return 0;
  514. }
  515. /* Cicada 820x */
  516. static struct phy_info phy_info_cis820x = {
  517. .phy_id = 0x000fc440,
  518. .name = "Cicada Cis8204",
  519. .phy_id_mask = 0x000fffc0,
  520. .features = MII_GBIT_FEATURES,
  521. .init = &cis820x_init,
  522. .config_aneg = &gbit_config_aneg,
  523. .read_status = &cis820x_read_status,
  524. .ack_interrupt = &cis820x_ack_interrupt,
  525. .config_intr = &cis820x_config_intr,
  526. };
  527. static struct phy_info phy_info_dm9161 = {
  528. .phy_id = 0x0181b880,
  529. .phy_id_mask = 0x0ffffff0,
  530. .name = "Davicom DM9161E",
  531. .init = dm9161_init,
  532. .config_aneg = dm9161_config_aneg,
  533. .read_status = dm9161_read_status,
  534. .close = dm9161_close,
  535. };
  536. static struct phy_info phy_info_dm9161a = {
  537. .phy_id = 0x0181b8a0,
  538. .phy_id_mask = 0x0ffffff0,
  539. .name = "Davicom DM9161A",
  540. .features = MII_BASIC_FEATURES,
  541. .init = dm9161_init,
  542. .config_aneg = dm9161_config_aneg,
  543. .read_status = dm9161_read_status,
  544. .ack_interrupt = dm9161_ack_interrupt,
  545. .config_intr = dm9161_config_intr,
  546. .close = dm9161_close,
  547. };
  548. static struct phy_info phy_info_marvell = {
  549. .phy_id = 0x01410c00,
  550. .phy_id_mask = 0xffffff00,
  551. .name = "Marvell 88E11x1",
  552. .features = MII_GBIT_FEATURES,
  553. .init = &marvell_init,
  554. .config_aneg = &marvell_config_aneg,
  555. .read_status = &marvell_read_status,
  556. .ack_interrupt = &marvell_ack_interrupt,
  557. .config_intr = &marvell_config_intr,
  558. };
  559. static struct phy_info phy_info_genmii = {
  560. .phy_id = 0x00000000,
  561. .phy_id_mask = 0x00000000,
  562. .name = "Generic MII",
  563. .features = MII_BASIC_FEATURES,
  564. .config_aneg = genmii_config_aneg,
  565. .read_status = genmii_read_status,
  566. };
  567. static struct phy_info *phy_info[] = {
  568. &phy_info_cis820x,
  569. &phy_info_marvell,
  570. &phy_info_dm9161,
  571. &phy_info_dm9161a,
  572. &phy_info_genmii,
  573. NULL
  574. };
  575. u16 phy_read(struct ugeth_mii_info *mii_info, u16 regnum)
  576. {
  577. u16 retval;
  578. unsigned long flags;
  579. ugphy_vdbg("%s: IN", __FUNCTION__);
  580. spin_lock_irqsave(&mii_info->mdio_lock, flags);
  581. retval = mii_info->mdio_read(mii_info->dev, mii_info->mii_id, regnum);
  582. spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
  583. return retval;
  584. }
  585. void phy_write(struct ugeth_mii_info *mii_info, u16 regnum, u16 val)
  586. {
  587. unsigned long flags;
  588. ugphy_vdbg("%s: IN", __FUNCTION__);
  589. spin_lock_irqsave(&mii_info->mdio_lock, flags);
  590. mii_info->mdio_write(mii_info->dev, mii_info->mii_id, regnum, val);
  591. spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
  592. }
  593. /* Use the PHY ID registers to determine what type of PHY is attached
  594. * to device dev. return a struct phy_info structure describing that PHY
  595. */
  596. struct phy_info *get_phy_info(struct ugeth_mii_info *mii_info)
  597. {
  598. u16 phy_reg;
  599. u32 phy_ID;
  600. int i;
  601. struct phy_info *theInfo = NULL;
  602. struct net_device *dev = mii_info->dev;
  603. ugphy_vdbg("%s: IN", __FUNCTION__);
  604. /* Grab the bits from PHYIR1, and put them in the upper half */
  605. phy_reg = phy_read(mii_info, MII_PHYSID1);
  606. phy_ID = (phy_reg & 0xffff) << 16;
  607. /* Grab the bits from PHYIR2, and put them in the lower half */
  608. phy_reg = phy_read(mii_info, MII_PHYSID2);
  609. phy_ID |= (phy_reg & 0xffff);
  610. /* loop through all the known PHY types, and find one that */
  611. /* matches the ID we read from the PHY. */
  612. for (i = 0; phy_info[i]; i++)
  613. if (phy_info[i]->phy_id == (phy_ID & phy_info[i]->phy_id_mask)){
  614. theInfo = phy_info[i];
  615. break;
  616. }
  617. /* This shouldn't happen, as we have generic PHY support */
  618. if (theInfo == NULL) {
  619. ugphy_info("%s: PHY id %x is not supported!", dev->name,
  620. phy_ID);
  621. return NULL;
  622. } else {
  623. ugphy_info("%s: PHY is %s (%x)", dev->name, theInfo->name,
  624. phy_ID);
  625. }
  626. return theInfo;
  627. }