at91_ether.c 37 KB

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