at91_ether.c 37 KB

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