at91_ether.c 34 KB

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