at91_ether.c 38 KB

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