at91_ether.c 37 KB

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