at91_ether.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Ethernet driver for the Atmel AT91RM9200 (Thunder)
  3. *
  4. * Copyright (C) 2003 SAN People (Pty) Ltd
  5. *
  6. * Based on an earlier Atmel EMAC macrocell driver by Atmel and Lineo Inc.
  7. * Initial version by Rick Bronson 01/11/2003
  8. *
  9. * Intel LXT971A PHY support by Christopher Bahns & David Knickerbocker
  10. * (Polaroid Corporation)
  11. *
  12. * Realtek RTL8201(B)L PHY support by Roman Avramenko <roman@imsystems.ru>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/mii.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/clk.h>
  29. #include <asm/io.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/mach-types.h>
  32. #include <asm/arch/at91rm9200_emac.h>
  33. #include <asm/arch/gpio.h>
  34. #include <asm/arch/board.h>
  35. #include "at91_ether.h"
  36. #define DRV_NAME "at91_ether"
  37. #define DRV_VERSION "1.0"
  38. #define LINK_POLL_INTERVAL (HZ)
  39. /* ..................................................................... */
  40. /*
  41. * Read from a EMAC register.
  42. */
  43. static inline unsigned long at91_emac_read(unsigned int reg)
  44. {
  45. void __iomem *emac_base = (void __iomem *)AT91_VA_BASE_EMAC;
  46. return __raw_readl(emac_base + reg);
  47. }
  48. /*
  49. * Write to a EMAC register.
  50. */
  51. static inline void at91_emac_write(unsigned int reg, unsigned long value)
  52. {
  53. void __iomem *emac_base = (void __iomem *)AT91_VA_BASE_EMAC;
  54. __raw_writel(value, emac_base + reg);
  55. }
  56. /* ........................... PHY INTERFACE ........................... */
  57. /*
  58. * Enable the MDIO bit in MAC control register
  59. * When not called from an interrupt-handler, access to the PHY must be
  60. * protected by a spinlock.
  61. */
  62. static void enable_mdi(void)
  63. {
  64. unsigned long ctl;
  65. ctl = at91_emac_read(AT91_EMAC_CTL);
  66. at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_MPE); /* enable management port */
  67. }
  68. /*
  69. * Disable the MDIO bit in the MAC control register
  70. */
  71. static void disable_mdi(void)
  72. {
  73. unsigned long ctl;
  74. ctl = at91_emac_read(AT91_EMAC_CTL);
  75. at91_emac_write(AT91_EMAC_CTL, ctl & ~AT91_EMAC_MPE); /* disable management port */
  76. }
  77. /*
  78. * Wait until the PHY operation is complete.
  79. */
  80. static inline void at91_phy_wait(void) {
  81. unsigned long timeout = jiffies + 2;
  82. while (!(at91_emac_read(AT91_EMAC_SR) & AT91_EMAC_SR_IDLE)) {
  83. if (time_after(jiffies, timeout)) {
  84. printk("at91_ether: MIO timeout\n");
  85. break;
  86. }
  87. cpu_relax();
  88. }
  89. }
  90. /*
  91. * Write value to the a PHY register
  92. * Note: MDI interface is assumed to already have been enabled.
  93. */
  94. static void write_phy(unsigned char phy_addr, unsigned char address, unsigned int value)
  95. {
  96. at91_emac_write(AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_W
  97. | ((phy_addr & 0x1f) << 23) | (address << 18) | (value & AT91_EMAC_DATA));
  98. /* Wait until IDLE bit in Network Status register is cleared */
  99. at91_phy_wait();
  100. }
  101. /*
  102. * Read value stored in a PHY register.
  103. * Note: MDI interface is assumed to already have been enabled.
  104. */
  105. static void read_phy(unsigned char phy_addr, unsigned char address, unsigned int *value)
  106. {
  107. at91_emac_write(AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_R
  108. | ((phy_addr & 0x1f) << 23) | (address << 18));
  109. /* Wait until IDLE bit in Network Status register is cleared */
  110. at91_phy_wait();
  111. *value = at91_emac_read(AT91_EMAC_MAN) & AT91_EMAC_DATA;
  112. }
  113. /* ........................... PHY MANAGEMENT .......................... */
  114. /*
  115. * Access the PHY to determine the current link speed and mode, and update the
  116. * MAC accordingly.
  117. * If no link or auto-negotiation is busy, then no changes are made.
  118. */
  119. static void update_linkspeed(struct net_device *dev, int silent)
  120. {
  121. struct at91_private *lp = netdev_priv(dev);
  122. unsigned int bmsr, bmcr, lpa, mac_cfg;
  123. unsigned int speed, duplex;
  124. if (!mii_link_ok(&lp->mii)) { /* no link */
  125. netif_carrier_off(dev);
  126. if (!silent)
  127. printk(KERN_INFO "%s: Link down.\n", dev->name);
  128. return;
  129. }
  130. /* Link up, or auto-negotiation still in progress */
  131. read_phy(lp->phy_address, MII_BMSR, &bmsr);
  132. read_phy(lp->phy_address, MII_BMCR, &bmcr);
  133. if (bmcr & BMCR_ANENABLE) { /* AutoNegotiation is enabled */
  134. if (!(bmsr & BMSR_ANEGCOMPLETE))
  135. return; /* Do nothing - another interrupt generated when negotiation complete */
  136. read_phy(lp->phy_address, MII_LPA, &lpa);
  137. if ((lpa & LPA_100FULL) || (lpa & LPA_100HALF)) speed = SPEED_100;
  138. else speed = SPEED_10;
  139. if ((lpa & LPA_100FULL) || (lpa & LPA_10FULL)) duplex = DUPLEX_FULL;
  140. else duplex = DUPLEX_HALF;
  141. } else {
  142. speed = (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
  143. duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
  144. }
  145. /* Update the MAC */
  146. mac_cfg = at91_emac_read(AT91_EMAC_CFG) & ~(AT91_EMAC_SPD | AT91_EMAC_FD);
  147. if (speed == SPEED_100) {
  148. if (duplex == DUPLEX_FULL) /* 100 Full Duplex */
  149. mac_cfg |= AT91_EMAC_SPD | AT91_EMAC_FD;
  150. else /* 100 Half Duplex */
  151. mac_cfg |= AT91_EMAC_SPD;
  152. } else {
  153. if (duplex == DUPLEX_FULL) /* 10 Full Duplex */
  154. mac_cfg |= AT91_EMAC_FD;
  155. else {} /* 10 Half Duplex */
  156. }
  157. at91_emac_write(AT91_EMAC_CFG, mac_cfg);
  158. if (!silent)
  159. printk(KERN_INFO "%s: Link now %i-%s\n", dev->name, speed, (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");
  160. netif_carrier_on(dev);
  161. }
  162. /*
  163. * Handle interrupts from the PHY
  164. */
  165. static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id)
  166. {
  167. struct net_device *dev = (struct net_device *) dev_id;
  168. struct at91_private *lp = netdev_priv(dev);
  169. unsigned int phy;
  170. /*
  171. * This hander is triggered on both edges, but the PHY chips expect
  172. * level-triggering. We therefore have to check if the PHY actually has
  173. * an IRQ pending.
  174. */
  175. enable_mdi();
  176. if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
  177. read_phy(lp->phy_address, MII_DSINTR_REG, &phy); /* ack interrupt in Davicom PHY */
  178. if (!(phy & (1 << 0)))
  179. goto done;
  180. }
  181. else if (lp->phy_type == MII_LXT971A_ID) {
  182. read_phy(lp->phy_address, MII_ISINTS_REG, &phy); /* ack interrupt in Intel PHY */
  183. if (!(phy & (1 << 2)))
  184. goto done;
  185. }
  186. else if (lp->phy_type == MII_BCM5221_ID) {
  187. read_phy(lp->phy_address, MII_BCMINTR_REG, &phy); /* ack interrupt in Broadcom PHY */
  188. if (!(phy & (1 << 0)))
  189. goto done;
  190. }
  191. else if (lp->phy_type == MII_KS8721_ID) {
  192. read_phy(lp->phy_address, MII_TPISTATUS, &phy); /* ack interrupt in Micrel PHY */
  193. if (!(phy & ((1 << 2) | 1)))
  194. goto done;
  195. }
  196. else if (lp->phy_type == MII_T78Q21x3_ID) { /* ack interrupt in Teridian PHY */
  197. read_phy(lp->phy_address, MII_T78Q21INT_REG, &phy);
  198. if (!(phy & ((1 << 2) | 1)))
  199. goto done;
  200. }
  201. else if (lp->phy_type == MII_DP83848_ID) {
  202. read_phy(lp->phy_address, MII_DPPHYSTS_REG, &phy); /* ack interrupt in DP83848 PHY */
  203. if (!(phy & (1 << 7)))
  204. goto done;
  205. }
  206. update_linkspeed(dev, 0);
  207. done:
  208. disable_mdi();
  209. return IRQ_HANDLED;
  210. }
  211. /*
  212. * Initialize and enable the PHY interrupt for link-state changes
  213. */
  214. static void enable_phyirq(struct net_device *dev)
  215. {
  216. struct at91_private *lp = netdev_priv(dev);
  217. unsigned int dsintr, irq_number;
  218. int status;
  219. irq_number = lp->board_data.phy_irq_pin;
  220. if (!irq_number) {
  221. /*
  222. * PHY doesn't have an IRQ pin (RTL8201, DP83847, AC101L),
  223. * or board does not have it connected.
  224. */
  225. mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
  226. return;
  227. }
  228. status = request_irq(irq_number, at91ether_phy_interrupt, 0, dev->name, dev);
  229. if (status) {
  230. printk(KERN_ERR "at91_ether: PHY IRQ %d request failed - status %d!\n", irq_number, status);
  231. return;
  232. }
  233. spin_lock_irq(&lp->lock);
  234. enable_mdi();
  235. if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) { /* for Davicom PHY */
  236. read_phy(lp->phy_address, MII_DSINTR_REG, &dsintr);
  237. dsintr = dsintr & ~0xf00; /* clear bits 8..11 */
  238. write_phy(lp->phy_address, MII_DSINTR_REG, dsintr);
  239. }
  240. else if (lp->phy_type == MII_LXT971A_ID) { /* for Intel PHY */
  241. read_phy(lp->phy_address, MII_ISINTE_REG, &dsintr);
  242. dsintr = dsintr | 0xf2; /* set bits 1, 4..7 */
  243. write_phy(lp->phy_address, MII_ISINTE_REG, dsintr);
  244. }
  245. else if (lp->phy_type == MII_BCM5221_ID) { /* for Broadcom PHY */
  246. dsintr = (1 << 15) | ( 1 << 14);
  247. write_phy(lp->phy_address, MII_BCMINTR_REG, dsintr);
  248. }
  249. else if (lp->phy_type == MII_KS8721_ID) { /* for Micrel PHY */
  250. dsintr = (1 << 10) | ( 1 << 8);
  251. write_phy(lp->phy_address, MII_TPISTATUS, dsintr);
  252. }
  253. else if (lp->phy_type == MII_T78Q21x3_ID) { /* for Teridian PHY */
  254. read_phy(lp->phy_address, MII_T78Q21INT_REG, &dsintr);
  255. dsintr = dsintr | 0x500; /* set bits 8, 10 */
  256. write_phy(lp->phy_address, MII_T78Q21INT_REG, dsintr);
  257. }
  258. else if (lp->phy_type == MII_DP83848_ID) { /* National Semiconductor DP83848 PHY */
  259. read_phy(lp->phy_address, MII_DPMISR_REG, &dsintr);
  260. dsintr = dsintr | 0x3c; /* set bits 2..5 */
  261. write_phy(lp->phy_address, MII_DPMISR_REG, dsintr);
  262. read_phy(lp->phy_address, MII_DPMICR_REG, &dsintr);
  263. dsintr = dsintr | 0x3; /* set bits 0,1 */
  264. write_phy(lp->phy_address, MII_DPMICR_REG, dsintr);
  265. }
  266. disable_mdi();
  267. spin_unlock_irq(&lp->lock);
  268. }
  269. /*
  270. * Disable the PHY interrupt
  271. */
  272. static void disable_phyirq(struct net_device *dev)
  273. {
  274. struct at91_private *lp = netdev_priv(dev);
  275. unsigned int dsintr;
  276. unsigned int irq_number;
  277. irq_number = lp->board_data.phy_irq_pin;
  278. if (!irq_number) {
  279. del_timer_sync(&lp->check_timer);
  280. return;
  281. }
  282. spin_lock_irq(&lp->lock);
  283. enable_mdi();
  284. if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) { /* for Davicom PHY */
  285. read_phy(lp->phy_address, MII_DSINTR_REG, &dsintr);
  286. dsintr = dsintr | 0xf00; /* set bits 8..11 */
  287. write_phy(lp->phy_address, MII_DSINTR_REG, dsintr);
  288. }
  289. else if (lp->phy_type == MII_LXT971A_ID) { /* for Intel PHY */
  290. read_phy(lp->phy_address, MII_ISINTE_REG, &dsintr);
  291. dsintr = dsintr & ~0xf2; /* clear bits 1, 4..7 */
  292. write_phy(lp->phy_address, MII_ISINTE_REG, dsintr);
  293. }
  294. else if (lp->phy_type == MII_BCM5221_ID) { /* for Broadcom PHY */
  295. read_phy(lp->phy_address, MII_BCMINTR_REG, &dsintr);
  296. dsintr = ~(1 << 14);
  297. write_phy(lp->phy_address, MII_BCMINTR_REG, dsintr);
  298. }
  299. else if (lp->phy_type == MII_KS8721_ID) { /* for Micrel PHY */
  300. read_phy(lp->phy_address, MII_TPISTATUS, &dsintr);
  301. dsintr = ~((1 << 10) | (1 << 8));
  302. write_phy(lp->phy_address, MII_TPISTATUS, dsintr);
  303. }
  304. else if (lp->phy_type == MII_T78Q21x3_ID) { /* for Teridian PHY */
  305. read_phy(lp->phy_address, MII_T78Q21INT_REG, &dsintr);
  306. dsintr = dsintr & ~0x500; /* clear bits 8, 10 */
  307. write_phy(lp->phy_address, MII_T78Q21INT_REG, dsintr);
  308. }
  309. else if (lp->phy_type == MII_DP83848_ID) { /* National Semiconductor DP83848 PHY */
  310. read_phy(lp->phy_address, MII_DPMICR_REG, &dsintr);
  311. dsintr = dsintr & ~0x3; /* clear bits 0, 1 */
  312. write_phy(lp->phy_address, MII_DPMICR_REG, dsintr);
  313. read_phy(lp->phy_address, MII_DPMISR_REG, &dsintr);
  314. dsintr = dsintr & ~0x3c; /* clear bits 2..5 */
  315. write_phy(lp->phy_address, MII_DPMISR_REG, dsintr);
  316. }
  317. disable_mdi();
  318. spin_unlock_irq(&lp->lock);
  319. free_irq(irq_number, dev); /* Free interrupt handler */
  320. }
  321. /*
  322. * Perform a software reset of the PHY.
  323. */
  324. #if 0
  325. static void reset_phy(struct net_device *dev)
  326. {
  327. struct at91_private *lp = netdev_priv(dev);
  328. unsigned int bmcr;
  329. spin_lock_irq(&lp->lock);
  330. enable_mdi();
  331. /* Perform PHY reset */
  332. write_phy(lp->phy_address, MII_BMCR, BMCR_RESET);
  333. /* Wait until PHY reset is complete */
  334. do {
  335. read_phy(lp->phy_address, MII_BMCR, &bmcr);
  336. } while (!(bmcr & BMCR_RESET));
  337. disable_mdi();
  338. spin_unlock_irq(&lp->lock);
  339. }
  340. #endif
  341. static void at91ether_check_link(unsigned long dev_id)
  342. {
  343. struct net_device *dev = (struct net_device *) dev_id;
  344. struct at91_private *lp = netdev_priv(dev);
  345. enable_mdi();
  346. update_linkspeed(dev, 1);
  347. disable_mdi();
  348. mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
  349. }
  350. /* ......................... ADDRESS MANAGEMENT ........................ */
  351. /*
  352. * NOTE: Your bootloader must always set the MAC address correctly before
  353. * booting into Linux.
  354. *
  355. * - It must always set the MAC address after reset, even if it doesn't
  356. * happen to access the Ethernet while it's booting. Some versions of
  357. * U-Boot on the AT91RM9200-DK do not do this.
  358. *
  359. * - Likewise it must store the addresses in the correct byte order.
  360. * MicroMonitor (uMon) on the CSB337 does this incorrectly (and
  361. * continues to do so, for bug-compatibility).
  362. */
  363. static short __init unpack_mac_address(struct net_device *dev, unsigned int hi, unsigned int lo)
  364. {
  365. char addr[6];
  366. if (machine_is_csb337()) {
  367. addr[5] = (lo & 0xff); /* The CSB337 bootloader stores the MAC the wrong-way around */
  368. addr[4] = (lo & 0xff00) >> 8;
  369. addr[3] = (lo & 0xff0000) >> 16;
  370. addr[2] = (lo & 0xff000000) >> 24;
  371. addr[1] = (hi & 0xff);
  372. addr[0] = (hi & 0xff00) >> 8;
  373. }
  374. else {
  375. addr[0] = (lo & 0xff);
  376. addr[1] = (lo & 0xff00) >> 8;
  377. addr[2] = (lo & 0xff0000) >> 16;
  378. addr[3] = (lo & 0xff000000) >> 24;
  379. addr[4] = (hi & 0xff);
  380. addr[5] = (hi & 0xff00) >> 8;
  381. }
  382. if (is_valid_ether_addr(addr)) {
  383. memcpy(dev->dev_addr, &addr, 6);
  384. return 1;
  385. }
  386. return 0;
  387. }
  388. /*
  389. * Set the ethernet MAC address in dev->dev_addr
  390. */
  391. static void __init get_mac_address(struct net_device *dev)
  392. {
  393. /* Check Specific-Address 1 */
  394. if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA1H), at91_emac_read(AT91_EMAC_SA1L)))
  395. return;
  396. /* Check Specific-Address 2 */
  397. if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA2H), at91_emac_read(AT91_EMAC_SA2L)))
  398. return;
  399. /* Check Specific-Address 3 */
  400. if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA3H), at91_emac_read(AT91_EMAC_SA3L)))
  401. return;
  402. /* Check Specific-Address 4 */
  403. if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA4H), at91_emac_read(AT91_EMAC_SA4L)))
  404. return;
  405. printk(KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
  406. }
  407. /*
  408. * Program the hardware MAC address from dev->dev_addr.
  409. */
  410. static void update_mac_address(struct net_device *dev)
  411. {
  412. at91_emac_write(AT91_EMAC_SA1L, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) | (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
  413. at91_emac_write(AT91_EMAC_SA1H, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
  414. at91_emac_write(AT91_EMAC_SA2L, 0);
  415. at91_emac_write(AT91_EMAC_SA2H, 0);
  416. }
  417. /*
  418. * Store the new hardware address in dev->dev_addr, and update the MAC.
  419. */
  420. static int set_mac_address(struct net_device *dev, void* addr)
  421. {
  422. struct sockaddr *address = addr;
  423. DECLARE_MAC_BUF(mac);
  424. if (!is_valid_ether_addr(address->sa_data))
  425. return -EADDRNOTAVAIL;
  426. memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
  427. update_mac_address(dev);
  428. printk("%s: Setting MAC address to %s\n", dev->name,
  429. print_mac(mac, dev->dev_addr));
  430. return 0;
  431. }
  432. static int inline hash_bit_value(int bitnr, __u8 *addr)
  433. {
  434. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  435. return 1;
  436. return 0;
  437. }
  438. /*
  439. * The hash address register is 64 bits long and takes up two locations in the memory map.
  440. * The least significant bits are stored in EMAC_HSL and the most significant
  441. * bits in EMAC_HSH.
  442. *
  443. * The unicast hash enable and the multicast hash enable bits in the network configuration
  444. * register enable the reception of hash matched frames. The destination address is
  445. * reduced to a 6 bit index into the 64 bit hash register using the following hash function.
  446. * The hash function is an exclusive or of every sixth bit of the destination address.
  447. * hash_index[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  448. * hash_index[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  449. * hash_index[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  450. * hash_index[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  451. * hash_index[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  452. * hash_index[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  453. * da[0] represents the least significant bit of the first byte received, that is, the multicast/
  454. * unicast indicator, and da[47] represents the most significant bit of the last byte
  455. * received.
  456. * If the hash index points to a bit that is set in the hash register then the frame will be
  457. * matched according to whether the frame is multicast or unicast.
  458. * A multicast match will be signalled if the multicast hash enable bit is set, da[0] is 1 and
  459. * the hash index points to a bit set in the hash register.
  460. * A unicast match will be signalled if the unicast hash enable bit is set, da[0] is 0 and the
  461. * hash index points to a bit set in the hash register.
  462. * To receive all multicast frames, the hash register should be set with all ones and the
  463. * multicast hash enable bit should be set in the network configuration register.
  464. */
  465. /*
  466. * Return the hash index value for the specified address.
  467. */
  468. static int hash_get_index(__u8 *addr)
  469. {
  470. int i, j, bitval;
  471. int hash_index = 0;
  472. for (j = 0; j < 6; j++) {
  473. for (i = 0, bitval = 0; i < 8; i++)
  474. bitval ^= hash_bit_value(i*6 + j, addr);
  475. hash_index |= (bitval << j);
  476. }
  477. return hash_index;
  478. }
  479. /*
  480. * Add multicast addresses to the internal multicast-hash table.
  481. */
  482. static void at91ether_sethashtable(struct net_device *dev)
  483. {
  484. struct dev_mc_list *curr;
  485. unsigned long mc_filter[2];
  486. unsigned int i, bitnr;
  487. mc_filter[0] = mc_filter[1] = 0;
  488. curr = dev->mc_list;
  489. for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
  490. if (!curr) break; /* unexpected end of list */
  491. bitnr = hash_get_index(curr->dmi_addr);
  492. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  493. }
  494. at91_emac_write(AT91_EMAC_HSL, mc_filter[0]);
  495. at91_emac_write(AT91_EMAC_HSH, mc_filter[1]);
  496. }
  497. /*
  498. * Enable/Disable promiscuous and multicast modes.
  499. */
  500. static void at91ether_set_rx_mode(struct net_device *dev)
  501. {
  502. unsigned long cfg;
  503. cfg = at91_emac_read(AT91_EMAC_CFG);
  504. if (dev->flags & IFF_PROMISC) /* Enable promiscuous mode */
  505. cfg |= AT91_EMAC_CAF;
  506. else if (dev->flags & (~IFF_PROMISC)) /* Disable promiscuous mode */
  507. cfg &= ~AT91_EMAC_CAF;
  508. if (dev->flags & IFF_ALLMULTI) { /* Enable all multicast mode */
  509. at91_emac_write(AT91_EMAC_HSH, -1);
  510. at91_emac_write(AT91_EMAC_HSL, -1);
  511. cfg |= AT91_EMAC_MTI;
  512. } else if (dev->mc_count > 0) { /* Enable specific multicasts */
  513. at91ether_sethashtable(dev);
  514. cfg |= AT91_EMAC_MTI;
  515. } else if (dev->flags & (~IFF_ALLMULTI)) { /* Disable all multicast mode */
  516. at91_emac_write(AT91_EMAC_HSH, 0);
  517. at91_emac_write(AT91_EMAC_HSL, 0);
  518. cfg &= ~AT91_EMAC_MTI;
  519. }
  520. at91_emac_write(AT91_EMAC_CFG, cfg);
  521. }
  522. /* ......................... ETHTOOL SUPPORT ........................... */
  523. static int mdio_read(struct net_device *dev, int phy_id, int location)
  524. {
  525. unsigned int value;
  526. read_phy(phy_id, location, &value);
  527. return value;
  528. }
  529. static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
  530. {
  531. write_phy(phy_id, location, value);
  532. }
  533. static int at91ether_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  534. {
  535. struct at91_private *lp = netdev_priv(dev);
  536. int ret;
  537. spin_lock_irq(&lp->lock);
  538. enable_mdi();
  539. ret = mii_ethtool_gset(&lp->mii, cmd);
  540. disable_mdi();
  541. spin_unlock_irq(&lp->lock);
  542. if (lp->phy_media == PORT_FIBRE) { /* override media type since mii.c doesn't know */
  543. cmd->supported = SUPPORTED_FIBRE;
  544. cmd->port = PORT_FIBRE;
  545. }
  546. return ret;
  547. }
  548. static int at91ether_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  549. {
  550. struct at91_private *lp = netdev_priv(dev);
  551. int ret;
  552. spin_lock_irq(&lp->lock);
  553. enable_mdi();
  554. ret = mii_ethtool_sset(&lp->mii, cmd);
  555. disable_mdi();
  556. spin_unlock_irq(&lp->lock);
  557. return ret;
  558. }
  559. static int at91ether_nwayreset(struct net_device *dev)
  560. {
  561. struct at91_private *lp = netdev_priv(dev);
  562. int ret;
  563. spin_lock_irq(&lp->lock);
  564. enable_mdi();
  565. ret = mii_nway_restart(&lp->mii);
  566. disable_mdi();
  567. spin_unlock_irq(&lp->lock);
  568. return ret;
  569. }
  570. static void at91ether_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  571. {
  572. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  573. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  574. strlcpy(info->bus_info, dev->dev.parent->bus_id, sizeof(info->bus_info));
  575. }
  576. static const struct ethtool_ops at91ether_ethtool_ops = {
  577. .get_settings = at91ether_get_settings,
  578. .set_settings = at91ether_set_settings,
  579. .get_drvinfo = at91ether_get_drvinfo,
  580. .nway_reset = at91ether_nwayreset,
  581. .get_link = ethtool_op_get_link,
  582. };
  583. static int at91ether_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  584. {
  585. struct at91_private *lp = netdev_priv(dev);
  586. int res;
  587. if (!netif_running(dev))
  588. return -EINVAL;
  589. spin_lock_irq(&lp->lock);
  590. enable_mdi();
  591. res = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
  592. disable_mdi();
  593. spin_unlock_irq(&lp->lock);
  594. return res;
  595. }
  596. /* ................................ MAC ................................ */
  597. /*
  598. * Initialize and start the Receiver and Transmit subsystems
  599. */
  600. static void at91ether_start(struct net_device *dev)
  601. {
  602. struct at91_private *lp = netdev_priv(dev);
  603. struct recv_desc_bufs *dlist, *dlist_phys;
  604. int i;
  605. unsigned long ctl;
  606. dlist = lp->dlist;
  607. dlist_phys = lp->dlist_phys;
  608. for (i = 0; i < MAX_RX_DESCR; i++) {
  609. dlist->descriptors[i].addr = (unsigned int) &dlist_phys->recv_buf[i][0];
  610. dlist->descriptors[i].size = 0;
  611. }
  612. /* Set the Wrap bit on the last descriptor */
  613. dlist->descriptors[i-1].addr |= EMAC_DESC_WRAP;
  614. /* Reset buffer index */
  615. lp->rxBuffIndex = 0;
  616. /* Program address of descriptor list in Rx Buffer Queue register */
  617. at91_emac_write(AT91_EMAC_RBQP, (unsigned long) dlist_phys);
  618. /* Enable Receive and Transmit */
  619. ctl = at91_emac_read(AT91_EMAC_CTL);
  620. at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_RE | AT91_EMAC_TE);
  621. }
  622. /*
  623. * Open the ethernet interface
  624. */
  625. static int at91ether_open(struct net_device *dev)
  626. {
  627. struct at91_private *lp = netdev_priv(dev);
  628. unsigned long ctl;
  629. if (!is_valid_ether_addr(dev->dev_addr))
  630. return -EADDRNOTAVAIL;
  631. clk_enable(lp->ether_clk); /* Re-enable Peripheral clock */
  632. /* Clear internal statistics */
  633. ctl = at91_emac_read(AT91_EMAC_CTL);
  634. at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_CSR);
  635. /* Update the MAC address (incase user has changed it) */
  636. update_mac_address(dev);
  637. /* Enable PHY interrupt */
  638. enable_phyirq(dev);
  639. /* Enable MAC interrupts */
  640. at91_emac_write(AT91_EMAC_IER, AT91_EMAC_RCOM | AT91_EMAC_RBNA
  641. | AT91_EMAC_TUND | AT91_EMAC_RTRY | AT91_EMAC_TCOM
  642. | AT91_EMAC_ROVR | AT91_EMAC_ABT);
  643. /* Determine current link speed */
  644. spin_lock_irq(&lp->lock);
  645. enable_mdi();
  646. update_linkspeed(dev, 0);
  647. disable_mdi();
  648. spin_unlock_irq(&lp->lock);
  649. at91ether_start(dev);
  650. netif_start_queue(dev);
  651. return 0;
  652. }
  653. /*
  654. * Close the interface
  655. */
  656. static int at91ether_close(struct net_device *dev)
  657. {
  658. struct at91_private *lp = netdev_priv(dev);
  659. unsigned long ctl;
  660. /* Disable Receiver and Transmitter */
  661. ctl = at91_emac_read(AT91_EMAC_CTL);
  662. at91_emac_write(AT91_EMAC_CTL, ctl & ~(AT91_EMAC_TE | AT91_EMAC_RE));
  663. /* Disable PHY interrupt */
  664. disable_phyirq(dev);
  665. /* Disable MAC interrupts */
  666. at91_emac_write(AT91_EMAC_IDR, AT91_EMAC_RCOM | AT91_EMAC_RBNA
  667. | AT91_EMAC_TUND | AT91_EMAC_RTRY | AT91_EMAC_TCOM
  668. | AT91_EMAC_ROVR | AT91_EMAC_ABT);
  669. netif_stop_queue(dev);
  670. clk_disable(lp->ether_clk); /* Disable Peripheral clock */
  671. return 0;
  672. }
  673. /*
  674. * Transmit packet.
  675. */
  676. static int at91ether_tx(struct sk_buff *skb, struct net_device *dev)
  677. {
  678. struct at91_private *lp = netdev_priv(dev);
  679. if (at91_emac_read(AT91_EMAC_TSR) & AT91_EMAC_TSR_BNQ) {
  680. netif_stop_queue(dev);
  681. /* Store packet information (to free when Tx completed) */
  682. lp->skb = skb;
  683. lp->skb_length = skb->len;
  684. lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
  685. lp->stats.tx_bytes += skb->len;
  686. /* Set address of the data in the Transmit Address register */
  687. at91_emac_write(AT91_EMAC_TAR, lp->skb_physaddr);
  688. /* Set length of the packet in the Transmit Control register */
  689. at91_emac_write(AT91_EMAC_TCR, skb->len);
  690. dev->trans_start = jiffies;
  691. } else {
  692. printk(KERN_ERR "at91_ether.c: at91ether_tx() called, but device is busy!\n");
  693. return 1; /* if we return anything but zero, dev.c:1055 calls kfree_skb(skb)
  694. on this skb, he also reports -ENETDOWN and printk's, so either
  695. we free and return(0) or don't free and return 1 */
  696. }
  697. return 0;
  698. }
  699. /*
  700. * Update the current statistics from the internal statistics registers.
  701. */
  702. static struct net_device_stats *at91ether_stats(struct net_device *dev)
  703. {
  704. struct at91_private *lp = netdev_priv(dev);
  705. int ale, lenerr, seqe, lcol, ecol;
  706. if (netif_running(dev)) {
  707. lp->stats.rx_packets += at91_emac_read(AT91_EMAC_OK); /* Good frames received */
  708. ale = at91_emac_read(AT91_EMAC_ALE);
  709. lp->stats.rx_frame_errors += ale; /* Alignment errors */
  710. lenerr = at91_emac_read(AT91_EMAC_ELR) + at91_emac_read(AT91_EMAC_USF);
  711. lp->stats.rx_length_errors += lenerr; /* Excessive Length or Undersize Frame error */
  712. seqe = at91_emac_read(AT91_EMAC_SEQE);
  713. lp->stats.rx_crc_errors += seqe; /* CRC error */
  714. lp->stats.rx_fifo_errors += at91_emac_read(AT91_EMAC_DRFC); /* Receive buffer not available */
  715. lp->stats.rx_errors += (ale + lenerr + seqe
  716. + at91_emac_read(AT91_EMAC_CDE) + at91_emac_read(AT91_EMAC_RJB));
  717. lp->stats.tx_packets += at91_emac_read(AT91_EMAC_FRA); /* Frames successfully transmitted */
  718. lp->stats.tx_fifo_errors += at91_emac_read(AT91_EMAC_TUE); /* Transmit FIFO underruns */
  719. lp->stats.tx_carrier_errors += at91_emac_read(AT91_EMAC_CSE); /* Carrier Sense errors */
  720. lp->stats.tx_heartbeat_errors += at91_emac_read(AT91_EMAC_SQEE);/* Heartbeat error */
  721. lcol = at91_emac_read(AT91_EMAC_LCOL);
  722. ecol = at91_emac_read(AT91_EMAC_ECOL);
  723. lp->stats.tx_window_errors += lcol; /* Late collisions */
  724. lp->stats.tx_aborted_errors += ecol; /* 16 collisions */
  725. lp->stats.collisions += (at91_emac_read(AT91_EMAC_SCOL) + at91_emac_read(AT91_EMAC_MCOL) + lcol + ecol);
  726. }
  727. return &lp->stats;
  728. }
  729. /*
  730. * Extract received frame from buffer descriptors and sent to upper layers.
  731. * (Called from interrupt context)
  732. */
  733. static void at91ether_rx(struct net_device *dev)
  734. {
  735. struct at91_private *lp = netdev_priv(dev);
  736. struct recv_desc_bufs *dlist;
  737. unsigned char *p_recv;
  738. struct sk_buff *skb;
  739. unsigned int pktlen;
  740. dlist = lp->dlist;
  741. while (dlist->descriptors[lp->rxBuffIndex].addr & EMAC_DESC_DONE) {
  742. p_recv = dlist->recv_buf[lp->rxBuffIndex];
  743. pktlen = dlist->descriptors[lp->rxBuffIndex].size & 0x7ff; /* Length of frame including FCS */
  744. skb = dev_alloc_skb(pktlen + 2);
  745. if (skb != NULL) {
  746. skb_reserve(skb, 2);
  747. memcpy(skb_put(skb, pktlen), p_recv, pktlen);
  748. skb->protocol = eth_type_trans(skb, dev);
  749. dev->last_rx = jiffies;
  750. lp->stats.rx_bytes += pktlen;
  751. netif_rx(skb);
  752. }
  753. else {
  754. lp->stats.rx_dropped += 1;
  755. printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
  756. }
  757. if (dlist->descriptors[lp->rxBuffIndex].size & EMAC_MULTICAST)
  758. lp->stats.multicast++;
  759. dlist->descriptors[lp->rxBuffIndex].addr &= ~EMAC_DESC_DONE; /* reset ownership bit */
  760. if (lp->rxBuffIndex == MAX_RX_DESCR-1) /* wrap after last buffer */
  761. lp->rxBuffIndex = 0;
  762. else
  763. lp->rxBuffIndex++;
  764. }
  765. }
  766. /*
  767. * MAC interrupt handler
  768. */
  769. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  770. {
  771. struct net_device *dev = (struct net_device *) dev_id;
  772. struct at91_private *lp = netdev_priv(dev);
  773. unsigned long intstatus, ctl;
  774. /* MAC Interrupt Status register indicates what interrupts are pending.
  775. It is automatically cleared once read. */
  776. intstatus = at91_emac_read(AT91_EMAC_ISR);
  777. if (intstatus & AT91_EMAC_RCOM) /* Receive complete */
  778. at91ether_rx(dev);
  779. if (intstatus & AT91_EMAC_TCOM) { /* Transmit complete */
  780. /* The TCOM bit is set even if the transmission failed. */
  781. if (intstatus & (AT91_EMAC_TUND | AT91_EMAC_RTRY))
  782. lp->stats.tx_errors += 1;
  783. if (lp->skb) {
  784. dev_kfree_skb_irq(lp->skb);
  785. lp->skb = NULL;
  786. dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
  787. }
  788. netif_wake_queue(dev);
  789. }
  790. /* Work-around for Errata #11 */
  791. if (intstatus & AT91_EMAC_RBNA) {
  792. ctl = at91_emac_read(AT91_EMAC_CTL);
  793. at91_emac_write(AT91_EMAC_CTL, ctl & ~AT91_EMAC_RE);
  794. at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_RE);
  795. }
  796. if (intstatus & AT91_EMAC_ROVR)
  797. printk("%s: ROVR error\n", dev->name);
  798. return IRQ_HANDLED;
  799. }
  800. #ifdef CONFIG_NET_POLL_CONTROLLER
  801. static void at91ether_poll_controller(struct net_device *dev)
  802. {
  803. unsigned long flags;
  804. local_irq_save(flags);
  805. at91ether_interrupt(dev->irq, dev);
  806. local_irq_restore(flags);
  807. }
  808. #endif
  809. /*
  810. * Initialize the ethernet interface
  811. */
  812. static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_address,
  813. struct platform_device *pdev, struct clk *ether_clk)
  814. {
  815. struct at91_eth_data *board_data = pdev->dev.platform_data;
  816. struct net_device *dev;
  817. struct at91_private *lp;
  818. unsigned int val;
  819. int res;
  820. DECLARE_MAC_BUF(mac);
  821. dev = alloc_etherdev(sizeof(struct at91_private));
  822. if (!dev)
  823. return -ENOMEM;
  824. dev->base_addr = AT91_VA_BASE_EMAC;
  825. dev->irq = AT91RM9200_ID_EMAC;
  826. /* Install the interrupt handler */
  827. if (request_irq(dev->irq, at91ether_interrupt, 0, dev->name, dev)) {
  828. free_netdev(dev);
  829. return -EBUSY;
  830. }
  831. /* Allocate memory for DMA Receive descriptors */
  832. lp = netdev_priv(dev);
  833. lp->dlist = (struct recv_desc_bufs *) dma_alloc_coherent(NULL, sizeof(struct recv_desc_bufs), (dma_addr_t *) &lp->dlist_phys, GFP_KERNEL);
  834. if (lp->dlist == NULL) {
  835. free_irq(dev->irq, dev);
  836. free_netdev(dev);
  837. return -ENOMEM;
  838. }
  839. lp->board_data = *board_data;
  840. lp->ether_clk = ether_clk;
  841. platform_set_drvdata(pdev, dev);
  842. spin_lock_init(&lp->lock);
  843. ether_setup(dev);
  844. dev->open = at91ether_open;
  845. dev->stop = at91ether_close;
  846. dev->hard_start_xmit = at91ether_tx;
  847. dev->get_stats = at91ether_stats;
  848. dev->set_multicast_list = at91ether_set_rx_mode;
  849. dev->set_mac_address = set_mac_address;
  850. dev->ethtool_ops = &at91ether_ethtool_ops;
  851. dev->do_ioctl = at91ether_ioctl;
  852. #ifdef CONFIG_NET_POLL_CONTROLLER
  853. dev->poll_controller = at91ether_poll_controller;
  854. #endif
  855. SET_NETDEV_DEV(dev, &pdev->dev);
  856. get_mac_address(dev); /* Get ethernet address and store it in dev->dev_addr */
  857. update_mac_address(dev); /* Program ethernet address into MAC */
  858. at91_emac_write(AT91_EMAC_CTL, 0);
  859. if (lp->board_data.is_rmii)
  860. at91_emac_write(AT91_EMAC_CFG, AT91_EMAC_CLK_DIV32 | AT91_EMAC_BIG | AT91_EMAC_RMII);
  861. else
  862. at91_emac_write(AT91_EMAC_CFG, AT91_EMAC_CLK_DIV32 | AT91_EMAC_BIG);
  863. /* Perform PHY-specific initialization */
  864. spin_lock_irq(&lp->lock);
  865. enable_mdi();
  866. if ((phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
  867. read_phy(phy_address, MII_DSCR_REG, &val);
  868. if ((val & (1 << 10)) == 0) /* DSCR bit 10 is 0 -- fiber mode */
  869. lp->phy_media = PORT_FIBRE;
  870. } else if (machine_is_csb337()) {
  871. /* mix link activity status into LED2 link state */
  872. write_phy(phy_address, MII_LEDCTRL_REG, 0x0d22);
  873. }
  874. disable_mdi();
  875. spin_unlock_irq(&lp->lock);
  876. lp->mii.dev = dev; /* Support for ethtool */
  877. lp->mii.mdio_read = mdio_read;
  878. lp->mii.mdio_write = mdio_write;
  879. lp->mii.phy_id = phy_address;
  880. lp->mii.phy_id_mask = 0x1f;
  881. lp->mii.reg_num_mask = 0x1f;
  882. lp->phy_type = phy_type; /* Type of PHY connected */
  883. lp->phy_address = phy_address; /* MDI address of PHY */
  884. /* Register the network interface */
  885. res = register_netdev(dev);
  886. if (res) {
  887. free_irq(dev->irq, dev);
  888. free_netdev(dev);
  889. dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
  890. return res;
  891. }
  892. /* Determine current link speed */
  893. spin_lock_irq(&lp->lock);
  894. enable_mdi();
  895. update_linkspeed(dev, 0);
  896. disable_mdi();
  897. spin_unlock_irq(&lp->lock);
  898. netif_carrier_off(dev); /* will be enabled in open() */
  899. /* If board has no PHY IRQ, use a timer to poll the PHY */
  900. if (!lp->board_data.phy_irq_pin) {
  901. init_timer(&lp->check_timer);
  902. lp->check_timer.data = (unsigned long)dev;
  903. lp->check_timer.function = at91ether_check_link;
  904. }
  905. /* Display ethernet banner */
  906. printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%s)\n",
  907. dev->name, (uint) dev->base_addr, dev->irq,
  908. at91_emac_read(AT91_EMAC_CFG) & AT91_EMAC_SPD ? "100-" : "10-",
  909. at91_emac_read(AT91_EMAC_CFG) & AT91_EMAC_FD ? "FullDuplex" : "HalfDuplex",
  910. print_mac(mac, dev->dev_addr));
  911. if ((phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID))
  912. printk(KERN_INFO "%s: Davicom 9161 PHY %s\n", dev->name, (lp->phy_media == PORT_FIBRE) ? "(Fiber)" : "(Copper)");
  913. else if (phy_type == MII_LXT971A_ID)
  914. printk(KERN_INFO "%s: Intel LXT971A PHY\n", dev->name);
  915. else if (phy_type == MII_RTL8201_ID)
  916. printk(KERN_INFO "%s: Realtek RTL8201(B)L PHY\n", dev->name);
  917. else if (phy_type == MII_BCM5221_ID)
  918. printk(KERN_INFO "%s: Broadcom BCM5221 PHY\n", dev->name);
  919. else if (phy_type == MII_DP83847_ID)
  920. printk(KERN_INFO "%s: National Semiconductor DP83847 PHY\n", dev->name);
  921. else if (phy_type == MII_DP83848_ID)
  922. printk(KERN_INFO "%s: National Semiconductor DP83848 PHY\n", dev->name);
  923. else if (phy_type == MII_AC101L_ID)
  924. printk(KERN_INFO "%s: Altima AC101L PHY\n", dev->name);
  925. else if (phy_type == MII_KS8721_ID)
  926. printk(KERN_INFO "%s: Micrel KS8721 PHY\n", dev->name);
  927. else if (phy_type == MII_T78Q21x3_ID)
  928. printk(KERN_INFO "%s: Teridian 78Q21x3 PHY\n", dev->name);
  929. else if (phy_type == MII_LAN83C185_ID)
  930. printk(KERN_INFO "%s: SMSC LAN83C185 PHY\n", dev->name);
  931. return 0;
  932. }
  933. /*
  934. * Detect MAC and PHY and perform initialization
  935. */
  936. static int __init at91ether_probe(struct platform_device *pdev)
  937. {
  938. unsigned int phyid1, phyid2;
  939. int detected = -1;
  940. unsigned long phy_id;
  941. unsigned short phy_address = 0;
  942. struct clk *ether_clk;
  943. ether_clk = clk_get(&pdev->dev, "ether_clk");
  944. if (IS_ERR(ether_clk)) {
  945. printk(KERN_ERR "at91_ether: no clock defined\n");
  946. return -ENODEV;
  947. }
  948. clk_enable(ether_clk); /* Enable Peripheral clock */
  949. while ((detected != 0) && (phy_address < 32)) {
  950. /* Read the PHY ID registers */
  951. enable_mdi();
  952. read_phy(phy_address, MII_PHYSID1, &phyid1);
  953. read_phy(phy_address, MII_PHYSID2, &phyid2);
  954. disable_mdi();
  955. phy_id = (phyid1 << 16) | (phyid2 & 0xfff0);
  956. switch (phy_id) {
  957. case MII_DM9161_ID: /* Davicom 9161: PHY_ID1 = 0x181, PHY_ID2 = B881 */
  958. case MII_DM9161A_ID: /* Davicom 9161A: PHY_ID1 = 0x181, PHY_ID2 = B8A0 */
  959. case MII_LXT971A_ID: /* Intel LXT971A: PHY_ID1 = 0x13, PHY_ID2 = 78E0 */
  960. case MII_RTL8201_ID: /* Realtek RTL8201: PHY_ID1 = 0, PHY_ID2 = 0x8201 */
  961. case MII_BCM5221_ID: /* Broadcom BCM5221: PHY_ID1 = 0x40, PHY_ID2 = 0x61e0 */
  962. case MII_DP83847_ID: /* National Semiconductor DP83847: */
  963. case MII_DP83848_ID: /* National Semiconductor DP83848: */
  964. case MII_AC101L_ID: /* Altima AC101L: PHY_ID1 = 0x22, PHY_ID2 = 0x5520 */
  965. case MII_KS8721_ID: /* Micrel KS8721: PHY_ID1 = 0x22, PHY_ID2 = 0x1610 */
  966. case MII_T78Q21x3_ID: /* Teridian 78Q21x3: PHY_ID1 = 0x0E, PHY_ID2 = 7237 */
  967. case MII_LAN83C185_ID: /* SMSC LAN83C185: PHY_ID1 = 0x0007, PHY_ID2 = 0xC0A1 */
  968. detected = at91ether_setup(phy_id, phy_address, pdev, ether_clk);
  969. break;
  970. }
  971. phy_address++;
  972. }
  973. clk_disable(ether_clk); /* Disable Peripheral clock */
  974. return detected;
  975. }
  976. static int __devexit at91ether_remove(struct platform_device *pdev)
  977. {
  978. struct net_device *dev = platform_get_drvdata(pdev);
  979. struct at91_private *lp = netdev_priv(dev);
  980. unregister_netdev(dev);
  981. free_irq(dev->irq, dev);
  982. dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
  983. clk_put(lp->ether_clk);
  984. platform_set_drvdata(pdev, NULL);
  985. free_netdev(dev);
  986. return 0;
  987. }
  988. #ifdef CONFIG_PM
  989. static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
  990. {
  991. struct net_device *net_dev = platform_get_drvdata(pdev);
  992. struct at91_private *lp = netdev_priv(net_dev);
  993. int phy_irq = lp->board_data.phy_irq_pin;
  994. if (netif_running(net_dev)) {
  995. if (phy_irq)
  996. disable_irq(phy_irq);
  997. netif_stop_queue(net_dev);
  998. netif_device_detach(net_dev);
  999. clk_disable(lp->ether_clk);
  1000. }
  1001. return 0;
  1002. }
  1003. static int at91ether_resume(struct platform_device *pdev)
  1004. {
  1005. struct net_device *net_dev = platform_get_drvdata(pdev);
  1006. struct at91_private *lp = netdev_priv(net_dev);
  1007. int phy_irq = lp->board_data.phy_irq_pin;
  1008. if (netif_running(net_dev)) {
  1009. clk_enable(lp->ether_clk);
  1010. netif_device_attach(net_dev);
  1011. netif_start_queue(net_dev);
  1012. if (phy_irq)
  1013. enable_irq(phy_irq);
  1014. }
  1015. return 0;
  1016. }
  1017. #else
  1018. #define at91ether_suspend NULL
  1019. #define at91ether_resume NULL
  1020. #endif
  1021. static struct platform_driver at91ether_driver = {
  1022. .probe = at91ether_probe,
  1023. .remove = __devexit_p(at91ether_remove),
  1024. .suspend = at91ether_suspend,
  1025. .resume = at91ether_resume,
  1026. .driver = {
  1027. .name = DRV_NAME,
  1028. .owner = THIS_MODULE,
  1029. },
  1030. };
  1031. static int __init at91ether_init(void)
  1032. {
  1033. return platform_driver_register(&at91ether_driver);
  1034. }
  1035. static void __exit at91ether_exit(void)
  1036. {
  1037. platform_driver_unregister(&at91ether_driver);
  1038. }
  1039. module_init(at91ether_init)
  1040. module_exit(at91ether_exit)
  1041. MODULE_LICENSE("GPL");
  1042. MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
  1043. MODULE_AUTHOR("Andrew Victor");
  1044. MODULE_ALIAS("platform:" DRV_NAME);