ucc_geth_phy.c 20 KB

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