at91_ether.c 34 KB

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