at91_ether.c 34 KB

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