at91_ether.c 35 KB

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