ucc_geth_phy.c 20 KB

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