ucc_geth_phy.c 20 KB

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