au1000_eth.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. *
  3. * Alchemy Au1x00 ethernet driver
  4. *
  5. * Copyright 2001-2003, 2006 MontaVista Software Inc.
  6. * Copyright 2002 TimeSys Corp.
  7. * Added ethtool/mii-tool support,
  8. * Copyright 2004 Matt Porter <mporter@kernel.crashing.org>
  9. * Update: 2004 Bjoern Riemer, riemer@fokus.fraunhofer.de
  10. * or riemer@riemer-nt.de: fixed the link beat detection with
  11. * ioctls (SIOCGMIIPHY)
  12. * Copyright 2006 Herbert Valerio Riedel <hvr@gnu.org>
  13. * converted to use linux-2.6.x's PHY framework
  14. *
  15. * Author: MontaVista Software, Inc.
  16. * ppopov@mvista.com or source@mvista.com
  17. *
  18. * ########################################################################
  19. *
  20. * This program is free software; you can distribute it and/or modify it
  21. * under the terms of the GNU General Public License (Version 2) as
  22. * published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope it will be useful, but WITHOUT
  25. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  26. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  27. * for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  32. *
  33. * ########################################################################
  34. *
  35. *
  36. */
  37. #include <linux/module.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched.h>
  40. #include <linux/string.h>
  41. #include <linux/timer.h>
  42. #include <linux/errno.h>
  43. #include <linux/in.h>
  44. #include <linux/ioport.h>
  45. #include <linux/bitops.h>
  46. #include <linux/slab.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/pci.h>
  49. #include <linux/init.h>
  50. #include <linux/netdevice.h>
  51. #include <linux/etherdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/mii.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/delay.h>
  56. #include <linux/crc32.h>
  57. #include <linux/phy.h>
  58. #include <asm/mipsregs.h>
  59. #include <asm/irq.h>
  60. #include <asm/io.h>
  61. #include <asm/processor.h>
  62. #include <asm/mach-au1x00/au1000.h>
  63. #include <asm/cpu.h>
  64. #include "au1000_eth.h"
  65. #ifdef AU1000_ETH_DEBUG
  66. static int au1000_debug = 5;
  67. #else
  68. static int au1000_debug = 3;
  69. #endif
  70. #define DRV_NAME "au1000_eth"
  71. #define DRV_VERSION "1.6"
  72. #define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
  73. #define DRV_DESC "Au1xxx on-chip Ethernet driver"
  74. MODULE_AUTHOR(DRV_AUTHOR);
  75. MODULE_DESCRIPTION(DRV_DESC);
  76. MODULE_LICENSE("GPL");
  77. // prototypes
  78. static void hard_stop(struct net_device *);
  79. static void enable_rx_tx(struct net_device *dev);
  80. static struct net_device * au1000_probe(int port_num);
  81. static int au1000_init(struct net_device *);
  82. static int au1000_open(struct net_device *);
  83. static int au1000_close(struct net_device *);
  84. static int au1000_tx(struct sk_buff *, struct net_device *);
  85. static int au1000_rx(struct net_device *);
  86. static irqreturn_t au1000_interrupt(int, void *);
  87. static void au1000_tx_timeout(struct net_device *);
  88. static void set_rx_mode(struct net_device *);
  89. static struct net_device_stats *au1000_get_stats(struct net_device *);
  90. static int au1000_ioctl(struct net_device *, struct ifreq *, int);
  91. static int mdio_read(struct net_device *, int, int);
  92. static void mdio_write(struct net_device *, int, int, u16);
  93. static void au1000_adjust_link(struct net_device *);
  94. static void enable_mac(struct net_device *, int);
  95. // externs
  96. extern int get_ethernet_addr(char *ethernet_addr);
  97. extern void str2eaddr(unsigned char *ea, unsigned char *str);
  98. extern char * prom_getcmdline(void);
  99. /*
  100. * Theory of operation
  101. *
  102. * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
  103. * There are four receive and four transmit descriptors. These
  104. * descriptors are not in memory; rather, they are just a set of
  105. * hardware registers.
  106. *
  107. * Since the Au1000 has a coherent data cache, the receive and
  108. * transmit buffers are allocated from the KSEG0 segment. The
  109. * hardware registers, however, are still mapped at KSEG1 to
  110. * make sure there's no out-of-order writes, and that all writes
  111. * complete immediately.
  112. */
  113. /* These addresses are only used if yamon doesn't tell us what
  114. * the mac address is, and the mac address is not passed on the
  115. * command line.
  116. */
  117. static unsigned char au1000_mac_addr[6] __devinitdata = {
  118. 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00
  119. };
  120. struct au1000_private *au_macs[NUM_ETH_INTERFACES];
  121. /*
  122. * board-specific configurations
  123. *
  124. * PHY detection algorithm
  125. *
  126. * If AU1XXX_PHY_STATIC_CONFIG is undefined, the PHY setup is
  127. * autodetected:
  128. *
  129. * mii_probe() first searches the current MAC's MII bus for a PHY,
  130. * selecting the first (or last, if AU1XXX_PHY_SEARCH_HIGHEST_ADDR is
  131. * defined) PHY address not already claimed by another netdev.
  132. *
  133. * If nothing was found that way when searching for the 2nd ethernet
  134. * controller's PHY and AU1XXX_PHY1_SEARCH_ON_MAC0 is defined, then
  135. * the first MII bus is searched as well for an unclaimed PHY; this is
  136. * needed in case of a dual-PHY accessible only through the MAC0's MII
  137. * bus.
  138. *
  139. * Finally, if no PHY is found, then the corresponding ethernet
  140. * controller is not registered to the network subsystem.
  141. */
  142. /* autodetection defaults */
  143. #undef AU1XXX_PHY_SEARCH_HIGHEST_ADDR
  144. #define AU1XXX_PHY1_SEARCH_ON_MAC0
  145. /* static PHY setup
  146. *
  147. * most boards PHY setup should be detectable properly with the
  148. * autodetection algorithm in mii_probe(), but in some cases (e.g. if
  149. * you have a switch attached, or want to use the PHY's interrupt
  150. * notification capabilities) you can provide a static PHY
  151. * configuration here
  152. *
  153. * IRQs may only be set, if a PHY address was configured
  154. * If a PHY address is given, also a bus id is required to be set
  155. *
  156. * ps: make sure the used irqs are configured properly in the board
  157. * specific irq-map
  158. */
  159. #if defined(CONFIG_MIPS_BOSPORUS)
  160. /*
  161. * Micrel/Kendin 5 port switch attached to MAC0,
  162. * MAC0 is associated with PHY address 5 (== WAN port)
  163. * MAC1 is not associated with any PHY, since it's connected directly
  164. * to the switch.
  165. * no interrupts are used
  166. */
  167. # define AU1XXX_PHY_STATIC_CONFIG
  168. # define AU1XXX_PHY0_ADDR 5
  169. # define AU1XXX_PHY0_BUSID 0
  170. # undef AU1XXX_PHY0_IRQ
  171. # undef AU1XXX_PHY1_ADDR
  172. # undef AU1XXX_PHY1_BUSID
  173. # undef AU1XXX_PHY1_IRQ
  174. #endif
  175. #if defined(AU1XXX_PHY0_BUSID) && (AU1XXX_PHY0_BUSID > 0)
  176. # error MAC0-associated PHY attached 2nd MACs MII bus not supported yet
  177. #endif
  178. /*
  179. * MII operations
  180. */
  181. static int mdio_read(struct net_device *dev, int phy_addr, int reg)
  182. {
  183. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  184. volatile u32 *const mii_control_reg = &aup->mac->mii_control;
  185. volatile u32 *const mii_data_reg = &aup->mac->mii_data;
  186. u32 timedout = 20;
  187. u32 mii_control;
  188. while (*mii_control_reg & MAC_MII_BUSY) {
  189. mdelay(1);
  190. if (--timedout == 0) {
  191. printk(KERN_ERR "%s: read_MII busy timeout!!\n",
  192. dev->name);
  193. return -1;
  194. }
  195. }
  196. mii_control = MAC_SET_MII_SELECT_REG(reg) |
  197. MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_READ;
  198. *mii_control_reg = mii_control;
  199. timedout = 20;
  200. while (*mii_control_reg & MAC_MII_BUSY) {
  201. mdelay(1);
  202. if (--timedout == 0) {
  203. printk(KERN_ERR "%s: mdio_read busy timeout!!\n",
  204. dev->name);
  205. return -1;
  206. }
  207. }
  208. return (int)*mii_data_reg;
  209. }
  210. static void mdio_write(struct net_device *dev, int phy_addr, int reg, u16 value)
  211. {
  212. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  213. volatile u32 *const mii_control_reg = &aup->mac->mii_control;
  214. volatile u32 *const mii_data_reg = &aup->mac->mii_data;
  215. u32 timedout = 20;
  216. u32 mii_control;
  217. while (*mii_control_reg & MAC_MII_BUSY) {
  218. mdelay(1);
  219. if (--timedout == 0) {
  220. printk(KERN_ERR "%s: mdio_write busy timeout!!\n",
  221. dev->name);
  222. return;
  223. }
  224. }
  225. mii_control = MAC_SET_MII_SELECT_REG(reg) |
  226. MAC_SET_MII_SELECT_PHY(phy_addr) | MAC_MII_WRITE;
  227. *mii_data_reg = value;
  228. *mii_control_reg = mii_control;
  229. }
  230. static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  231. {
  232. /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
  233. * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */
  234. struct net_device *const dev = bus->priv;
  235. enable_mac(dev, 0); /* make sure the MAC associated with this
  236. * mii_bus is enabled */
  237. return mdio_read(dev, phy_addr, regnum);
  238. }
  239. static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  240. u16 value)
  241. {
  242. struct net_device *const dev = bus->priv;
  243. enable_mac(dev, 0); /* make sure the MAC associated with this
  244. * mii_bus is enabled */
  245. mdio_write(dev, phy_addr, regnum, value);
  246. return 0;
  247. }
  248. static int mdiobus_reset(struct mii_bus *bus)
  249. {
  250. struct net_device *const dev = bus->priv;
  251. enable_mac(dev, 0); /* make sure the MAC associated with this
  252. * mii_bus is enabled */
  253. return 0;
  254. }
  255. static int mii_probe (struct net_device *dev)
  256. {
  257. struct au1000_private *const aup = (struct au1000_private *) dev->priv;
  258. struct phy_device *phydev = NULL;
  259. #if defined(AU1XXX_PHY_STATIC_CONFIG)
  260. BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
  261. if(aup->mac_id == 0) { /* get PHY0 */
  262. # if defined(AU1XXX_PHY0_ADDR)
  263. phydev = au_macs[AU1XXX_PHY0_BUSID]->mii_bus.phy_map[AU1XXX_PHY0_ADDR];
  264. # else
  265. printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n",
  266. dev->name);
  267. return 0;
  268. # endif /* defined(AU1XXX_PHY0_ADDR) */
  269. } else if (aup->mac_id == 1) { /* get PHY1 */
  270. # if defined(AU1XXX_PHY1_ADDR)
  271. phydev = au_macs[AU1XXX_PHY1_BUSID]->mii_bus.phy_map[AU1XXX_PHY1_ADDR];
  272. # else
  273. printk (KERN_INFO DRV_NAME ":%s: using PHY-less setup\n",
  274. dev->name);
  275. return 0;
  276. # endif /* defined(AU1XXX_PHY1_ADDR) */
  277. }
  278. #else /* defined(AU1XXX_PHY_STATIC_CONFIG) */
  279. int phy_addr;
  280. /* find the first (lowest address) PHY on the current MAC's MII bus */
  281. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
  282. if (aup->mii_bus.phy_map[phy_addr]) {
  283. phydev = aup->mii_bus.phy_map[phy_addr];
  284. # if !defined(AU1XXX_PHY_SEARCH_HIGHEST_ADDR)
  285. break; /* break out with first one found */
  286. # endif
  287. }
  288. # if defined(AU1XXX_PHY1_SEARCH_ON_MAC0)
  289. /* try harder to find a PHY */
  290. if (!phydev && (aup->mac_id == 1)) {
  291. /* no PHY found, maybe we have a dual PHY? */
  292. printk (KERN_INFO DRV_NAME ": no PHY found on MAC1, "
  293. "let's see if it's attached to MAC0...\n");
  294. BUG_ON(!au_macs[0]);
  295. /* find the first (lowest address) non-attached PHY on
  296. * the MAC0 MII bus */
  297. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  298. struct phy_device *const tmp_phydev =
  299. au_macs[0]->mii_bus.phy_map[phy_addr];
  300. if (!tmp_phydev)
  301. continue; /* no PHY here... */
  302. if (tmp_phydev->attached_dev)
  303. continue; /* already claimed by MAC0 */
  304. phydev = tmp_phydev;
  305. break; /* found it */
  306. }
  307. }
  308. # endif /* defined(AU1XXX_PHY1_SEARCH_OTHER_BUS) */
  309. #endif /* defined(AU1XXX_PHY_STATIC_CONFIG) */
  310. if (!phydev) {
  311. printk (KERN_ERR DRV_NAME ":%s: no PHY found\n", dev->name);
  312. return -1;
  313. }
  314. /* now we are supposed to have a proper phydev, to attach to... */
  315. BUG_ON(!phydev);
  316. BUG_ON(phydev->attached_dev);
  317. phydev = phy_connect(dev, phydev->dev.bus_id, &au1000_adjust_link, 0,
  318. PHY_INTERFACE_MODE_MII);
  319. if (IS_ERR(phydev)) {
  320. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  321. return PTR_ERR(phydev);
  322. }
  323. /* mask with MAC supported features */
  324. phydev->supported &= (SUPPORTED_10baseT_Half
  325. | SUPPORTED_10baseT_Full
  326. | SUPPORTED_100baseT_Half
  327. | SUPPORTED_100baseT_Full
  328. | SUPPORTED_Autoneg
  329. /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
  330. | SUPPORTED_MII
  331. | SUPPORTED_TP);
  332. phydev->advertising = phydev->supported;
  333. aup->old_link = 0;
  334. aup->old_speed = 0;
  335. aup->old_duplex = -1;
  336. aup->phy_dev = phydev;
  337. printk(KERN_INFO "%s: attached PHY driver [%s] "
  338. "(mii_bus:phy_addr=%s, irq=%d)\n",
  339. dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
  340. return 0;
  341. }
  342. /*
  343. * Buffer allocation/deallocation routines. The buffer descriptor returned
  344. * has the virtual and dma address of a buffer suitable for
  345. * both, receive and transmit operations.
  346. */
  347. static db_dest_t *GetFreeDB(struct au1000_private *aup)
  348. {
  349. db_dest_t *pDB;
  350. pDB = aup->pDBfree;
  351. if (pDB) {
  352. aup->pDBfree = pDB->pnext;
  353. }
  354. return pDB;
  355. }
  356. void ReleaseDB(struct au1000_private *aup, db_dest_t *pDB)
  357. {
  358. db_dest_t *pDBfree = aup->pDBfree;
  359. if (pDBfree)
  360. pDBfree->pnext = pDB;
  361. aup->pDBfree = pDB;
  362. }
  363. static void enable_rx_tx(struct net_device *dev)
  364. {
  365. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  366. if (au1000_debug > 4)
  367. printk(KERN_INFO "%s: enable_rx_tx\n", dev->name);
  368. aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
  369. au_sync_delay(10);
  370. }
  371. static void hard_stop(struct net_device *dev)
  372. {
  373. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  374. if (au1000_debug > 4)
  375. printk(KERN_INFO "%s: hard stop\n", dev->name);
  376. aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
  377. au_sync_delay(10);
  378. }
  379. static void enable_mac(struct net_device *dev, int force_reset)
  380. {
  381. unsigned long flags;
  382. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  383. spin_lock_irqsave(&aup->lock, flags);
  384. if(force_reset || (!aup->mac_enabled)) {
  385. *aup->enable = MAC_EN_CLOCK_ENABLE;
  386. au_sync_delay(2);
  387. *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
  388. | MAC_EN_CLOCK_ENABLE);
  389. au_sync_delay(2);
  390. aup->mac_enabled = 1;
  391. }
  392. spin_unlock_irqrestore(&aup->lock, flags);
  393. }
  394. static void reset_mac_unlocked(struct net_device *dev)
  395. {
  396. struct au1000_private *const aup = (struct au1000_private *) dev->priv;
  397. int i;
  398. hard_stop(dev);
  399. *aup->enable = MAC_EN_CLOCK_ENABLE;
  400. au_sync_delay(2);
  401. *aup->enable = 0;
  402. au_sync_delay(2);
  403. aup->tx_full = 0;
  404. for (i = 0; i < NUM_RX_DMA; i++) {
  405. /* reset control bits */
  406. aup->rx_dma_ring[i]->buff_stat &= ~0xf;
  407. }
  408. for (i = 0; i < NUM_TX_DMA; i++) {
  409. /* reset control bits */
  410. aup->tx_dma_ring[i]->buff_stat &= ~0xf;
  411. }
  412. aup->mac_enabled = 0;
  413. }
  414. static void reset_mac(struct net_device *dev)
  415. {
  416. struct au1000_private *const aup = (struct au1000_private *) dev->priv;
  417. unsigned long flags;
  418. if (au1000_debug > 4)
  419. printk(KERN_INFO "%s: reset mac, aup %x\n",
  420. dev->name, (unsigned)aup);
  421. spin_lock_irqsave(&aup->lock, flags);
  422. reset_mac_unlocked (dev);
  423. spin_unlock_irqrestore(&aup->lock, flags);
  424. }
  425. /*
  426. * Setup the receive and transmit "rings". These pointers are the addresses
  427. * of the rx and tx MAC DMA registers so they are fixed by the hardware --
  428. * these are not descriptors sitting in memory.
  429. */
  430. static void
  431. setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
  432. {
  433. int i;
  434. for (i = 0; i < NUM_RX_DMA; i++) {
  435. aup->rx_dma_ring[i] =
  436. (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i);
  437. }
  438. for (i = 0; i < NUM_TX_DMA; i++) {
  439. aup->tx_dma_ring[i] =
  440. (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i);
  441. }
  442. }
  443. static struct {
  444. u32 base_addr;
  445. u32 macen_addr;
  446. int irq;
  447. struct net_device *dev;
  448. } iflist[2] = {
  449. #ifdef CONFIG_SOC_AU1000
  450. {AU1000_ETH0_BASE, AU1000_MAC0_ENABLE, AU1000_MAC0_DMA_INT},
  451. {AU1000_ETH1_BASE, AU1000_MAC1_ENABLE, AU1000_MAC1_DMA_INT}
  452. #endif
  453. #ifdef CONFIG_SOC_AU1100
  454. {AU1100_ETH0_BASE, AU1100_MAC0_ENABLE, AU1100_MAC0_DMA_INT}
  455. #endif
  456. #ifdef CONFIG_SOC_AU1500
  457. {AU1500_ETH0_BASE, AU1500_MAC0_ENABLE, AU1500_MAC0_DMA_INT},
  458. {AU1500_ETH1_BASE, AU1500_MAC1_ENABLE, AU1500_MAC1_DMA_INT}
  459. #endif
  460. #ifdef CONFIG_SOC_AU1550
  461. {AU1550_ETH0_BASE, AU1550_MAC0_ENABLE, AU1550_MAC0_DMA_INT},
  462. {AU1550_ETH1_BASE, AU1550_MAC1_ENABLE, AU1550_MAC1_DMA_INT}
  463. #endif
  464. };
  465. static int num_ifs;
  466. /*
  467. * Setup the base address and interupt of the Au1xxx ethernet macs
  468. * based on cpu type and whether the interface is enabled in sys_pinfunc
  469. * register. The last interface is enabled if SYS_PF_NI2 (bit 4) is 0.
  470. */
  471. static int __init au1000_init_module(void)
  472. {
  473. int ni = (int)((au_readl(SYS_PINFUNC) & (u32)(SYS_PF_NI2)) >> 4);
  474. struct net_device *dev;
  475. int i, found_one = 0;
  476. num_ifs = NUM_ETH_INTERFACES - ni;
  477. for(i = 0; i < num_ifs; i++) {
  478. dev = au1000_probe(i);
  479. iflist[i].dev = dev;
  480. if (dev)
  481. found_one++;
  482. }
  483. if (!found_one)
  484. return -ENODEV;
  485. return 0;
  486. }
  487. /*
  488. * ethtool operations
  489. */
  490. static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  491. {
  492. struct au1000_private *aup = (struct au1000_private *)dev->priv;
  493. if (aup->phy_dev)
  494. return phy_ethtool_gset(aup->phy_dev, cmd);
  495. return -EINVAL;
  496. }
  497. static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  498. {
  499. struct au1000_private *aup = (struct au1000_private *)dev->priv;
  500. if (!capable(CAP_NET_ADMIN))
  501. return -EPERM;
  502. if (aup->phy_dev)
  503. return phy_ethtool_sset(aup->phy_dev, cmd);
  504. return -EINVAL;
  505. }
  506. static void
  507. au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  508. {
  509. struct au1000_private *aup = (struct au1000_private *)dev->priv;
  510. strcpy(info->driver, DRV_NAME);
  511. strcpy(info->version, DRV_VERSION);
  512. info->fw_version[0] = '\0';
  513. sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
  514. info->regdump_len = 0;
  515. }
  516. static const struct ethtool_ops au1000_ethtool_ops = {
  517. .get_settings = au1000_get_settings,
  518. .set_settings = au1000_set_settings,
  519. .get_drvinfo = au1000_get_drvinfo,
  520. .get_link = ethtool_op_get_link,
  521. };
  522. static struct net_device * au1000_probe(int port_num)
  523. {
  524. static unsigned version_printed = 0;
  525. struct au1000_private *aup = NULL;
  526. struct net_device *dev = NULL;
  527. db_dest_t *pDB, *pDBfree;
  528. char *pmac, *argptr;
  529. char ethaddr[6];
  530. int irq, i, err;
  531. u32 base, macen;
  532. if (port_num >= NUM_ETH_INTERFACES)
  533. return NULL;
  534. base = CPHYSADDR(iflist[port_num].base_addr );
  535. macen = CPHYSADDR(iflist[port_num].macen_addr);
  536. irq = iflist[port_num].irq;
  537. if (!request_mem_region( base, MAC_IOSIZE, "Au1x00 ENET") ||
  538. !request_mem_region(macen, 4, "Au1x00 ENET"))
  539. return NULL;
  540. if (version_printed++ == 0)
  541. printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
  542. dev = alloc_etherdev(sizeof(struct au1000_private));
  543. if (!dev) {
  544. printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME);
  545. return NULL;
  546. }
  547. if ((err = register_netdev(dev)) != 0) {
  548. printk(KERN_ERR "%s: Cannot register net device, error %d\n",
  549. DRV_NAME, err);
  550. free_netdev(dev);
  551. return NULL;
  552. }
  553. printk("%s: Au1xx0 Ethernet found at 0x%x, irq %d\n",
  554. dev->name, base, irq);
  555. aup = dev->priv;
  556. /* Allocate the data buffers */
  557. /* Snooping works fine with eth on all au1xxx */
  558. aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
  559. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  560. &aup->dma_addr, 0);
  561. if (!aup->vaddr) {
  562. free_netdev(dev);
  563. release_mem_region( base, MAC_IOSIZE);
  564. release_mem_region(macen, 4);
  565. return NULL;
  566. }
  567. /* aup->mac is the base address of the MAC's registers */
  568. aup->mac = (volatile mac_reg_t *)iflist[port_num].base_addr;
  569. /* Setup some variables for quick register address access */
  570. aup->enable = (volatile u32 *)iflist[port_num].macen_addr;
  571. aup->mac_id = port_num;
  572. au_macs[port_num] = aup;
  573. if (port_num == 0) {
  574. /* Check the environment variables first */
  575. if (get_ethernet_addr(ethaddr) == 0)
  576. memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
  577. else {
  578. /* Check command line */
  579. argptr = prom_getcmdline();
  580. if ((pmac = strstr(argptr, "ethaddr=")) == NULL)
  581. printk(KERN_INFO "%s: No MAC address found\n",
  582. dev->name);
  583. /* Use the hard coded MAC addresses */
  584. else {
  585. str2eaddr(ethaddr, pmac + strlen("ethaddr="));
  586. memcpy(au1000_mac_addr, ethaddr,
  587. sizeof(au1000_mac_addr));
  588. }
  589. }
  590. setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
  591. } else if (port_num == 1)
  592. setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
  593. /*
  594. * Assign to the Ethernet ports two consecutive MAC addresses
  595. * to match those that are printed on their stickers
  596. */
  597. memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
  598. dev->dev_addr[5] += port_num;
  599. *aup->enable = 0;
  600. aup->mac_enabled = 0;
  601. aup->mii_bus.priv = dev;
  602. aup->mii_bus.read = mdiobus_read;
  603. aup->mii_bus.write = mdiobus_write;
  604. aup->mii_bus.reset = mdiobus_reset;
  605. aup->mii_bus.name = "au1000_eth_mii";
  606. aup->mii_bus.id = aup->mac_id;
  607. aup->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  608. for(i = 0; i < PHY_MAX_ADDR; ++i)
  609. aup->mii_bus.irq[i] = PHY_POLL;
  610. /* if known, set corresponding PHY IRQs */
  611. #if defined(AU1XXX_PHY_STATIC_CONFIG)
  612. # if defined(AU1XXX_PHY0_IRQ)
  613. if (AU1XXX_PHY0_BUSID == aup->mii_bus.id)
  614. aup->mii_bus.irq[AU1XXX_PHY0_ADDR] = AU1XXX_PHY0_IRQ;
  615. # endif
  616. # if defined(AU1XXX_PHY1_IRQ)
  617. if (AU1XXX_PHY1_BUSID == aup->mii_bus.id)
  618. aup->mii_bus.irq[AU1XXX_PHY1_ADDR] = AU1XXX_PHY1_IRQ;
  619. # endif
  620. #endif
  621. mdiobus_register(&aup->mii_bus);
  622. if (mii_probe(dev) != 0) {
  623. goto err_out;
  624. }
  625. pDBfree = NULL;
  626. /* setup the data buffer descriptors and attach a buffer to each one */
  627. pDB = aup->db;
  628. for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
  629. pDB->pnext = pDBfree;
  630. pDBfree = pDB;
  631. pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
  632. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  633. pDB++;
  634. }
  635. aup->pDBfree = pDBfree;
  636. for (i = 0; i < NUM_RX_DMA; i++) {
  637. pDB = GetFreeDB(aup);
  638. if (!pDB) {
  639. goto err_out;
  640. }
  641. aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  642. aup->rx_db_inuse[i] = pDB;
  643. }
  644. for (i = 0; i < NUM_TX_DMA; i++) {
  645. pDB = GetFreeDB(aup);
  646. if (!pDB) {
  647. goto err_out;
  648. }
  649. aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  650. aup->tx_dma_ring[i]->len = 0;
  651. aup->tx_db_inuse[i] = pDB;
  652. }
  653. spin_lock_init(&aup->lock);
  654. dev->base_addr = base;
  655. dev->irq = irq;
  656. dev->open = au1000_open;
  657. dev->hard_start_xmit = au1000_tx;
  658. dev->stop = au1000_close;
  659. dev->get_stats = au1000_get_stats;
  660. dev->set_multicast_list = &set_rx_mode;
  661. dev->do_ioctl = &au1000_ioctl;
  662. SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
  663. dev->tx_timeout = au1000_tx_timeout;
  664. dev->watchdog_timeo = ETH_TX_TIMEOUT;
  665. /*
  666. * The boot code uses the ethernet controller, so reset it to start
  667. * fresh. au1000_init() expects that the device is in reset state.
  668. */
  669. reset_mac(dev);
  670. return dev;
  671. err_out:
  672. /* here we should have a valid dev plus aup-> register addresses
  673. * so we can reset the mac properly.*/
  674. reset_mac(dev);
  675. for (i = 0; i < NUM_RX_DMA; i++) {
  676. if (aup->rx_db_inuse[i])
  677. ReleaseDB(aup, aup->rx_db_inuse[i]);
  678. }
  679. for (i = 0; i < NUM_TX_DMA; i++) {
  680. if (aup->tx_db_inuse[i])
  681. ReleaseDB(aup, aup->tx_db_inuse[i]);
  682. }
  683. dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
  684. (void *)aup->vaddr, aup->dma_addr);
  685. unregister_netdev(dev);
  686. free_netdev(dev);
  687. release_mem_region( base, MAC_IOSIZE);
  688. release_mem_region(macen, 4);
  689. return NULL;
  690. }
  691. /*
  692. * Initialize the interface.
  693. *
  694. * When the device powers up, the clocks are disabled and the
  695. * mac is in reset state. When the interface is closed, we
  696. * do the same -- reset the device and disable the clocks to
  697. * conserve power. Thus, whenever au1000_init() is called,
  698. * the device should already be in reset state.
  699. */
  700. static int au1000_init(struct net_device *dev)
  701. {
  702. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  703. u32 flags;
  704. int i;
  705. u32 control;
  706. if (au1000_debug > 4)
  707. printk("%s: au1000_init\n", dev->name);
  708. /* bring the device out of reset */
  709. enable_mac(dev, 1);
  710. spin_lock_irqsave(&aup->lock, flags);
  711. aup->mac->control = 0;
  712. aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
  713. aup->tx_tail = aup->tx_head;
  714. aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
  715. aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
  716. aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
  717. dev->dev_addr[1]<<8 | dev->dev_addr[0];
  718. for (i = 0; i < NUM_RX_DMA; i++) {
  719. aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
  720. }
  721. au_sync();
  722. control = MAC_RX_ENABLE | MAC_TX_ENABLE;
  723. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  724. control |= MAC_BIG_ENDIAN;
  725. #endif
  726. if (aup->phy_dev) {
  727. if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
  728. control |= MAC_FULL_DUPLEX;
  729. else
  730. control |= MAC_DISABLE_RX_OWN;
  731. } else { /* PHY-less op, assume full-duplex */
  732. control |= MAC_FULL_DUPLEX;
  733. }
  734. aup->mac->control = control;
  735. aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
  736. au_sync();
  737. spin_unlock_irqrestore(&aup->lock, flags);
  738. return 0;
  739. }
  740. static void
  741. au1000_adjust_link(struct net_device *dev)
  742. {
  743. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  744. struct phy_device *phydev = aup->phy_dev;
  745. unsigned long flags;
  746. int status_change = 0;
  747. BUG_ON(!aup->phy_dev);
  748. spin_lock_irqsave(&aup->lock, flags);
  749. if (phydev->link && (aup->old_speed != phydev->speed)) {
  750. // speed changed
  751. switch(phydev->speed) {
  752. case SPEED_10:
  753. case SPEED_100:
  754. break;
  755. default:
  756. printk(KERN_WARNING
  757. "%s: Speed (%d) is not 10/100 ???\n",
  758. dev->name, phydev->speed);
  759. break;
  760. }
  761. aup->old_speed = phydev->speed;
  762. status_change = 1;
  763. }
  764. if (phydev->link && (aup->old_duplex != phydev->duplex)) {
  765. // duplex mode changed
  766. /* switching duplex mode requires to disable rx and tx! */
  767. hard_stop(dev);
  768. if (DUPLEX_FULL == phydev->duplex)
  769. aup->mac->control = ((aup->mac->control
  770. | MAC_FULL_DUPLEX)
  771. & ~MAC_DISABLE_RX_OWN);
  772. else
  773. aup->mac->control = ((aup->mac->control
  774. & ~MAC_FULL_DUPLEX)
  775. | MAC_DISABLE_RX_OWN);
  776. au_sync_delay(1);
  777. enable_rx_tx(dev);
  778. aup->old_duplex = phydev->duplex;
  779. status_change = 1;
  780. }
  781. if(phydev->link != aup->old_link) {
  782. // link state changed
  783. if (phydev->link) // link went up
  784. netif_schedule(dev);
  785. else { // link went down
  786. aup->old_speed = 0;
  787. aup->old_duplex = -1;
  788. }
  789. aup->old_link = phydev->link;
  790. status_change = 1;
  791. }
  792. spin_unlock_irqrestore(&aup->lock, flags);
  793. if (status_change) {
  794. if (phydev->link)
  795. printk(KERN_INFO "%s: link up (%d/%s)\n",
  796. dev->name, phydev->speed,
  797. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  798. else
  799. printk(KERN_INFO "%s: link down\n", dev->name);
  800. }
  801. }
  802. static int au1000_open(struct net_device *dev)
  803. {
  804. int retval;
  805. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  806. if (au1000_debug > 4)
  807. printk("%s: open: dev=%p\n", dev->name, dev);
  808. if ((retval = request_irq(dev->irq, &au1000_interrupt, 0,
  809. dev->name, dev))) {
  810. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  811. dev->name, dev->irq);
  812. return retval;
  813. }
  814. if ((retval = au1000_init(dev))) {
  815. printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
  816. free_irq(dev->irq, dev);
  817. return retval;
  818. }
  819. if (aup->phy_dev) {
  820. /* cause the PHY state machine to schedule a link state check */
  821. aup->phy_dev->state = PHY_CHANGELINK;
  822. phy_start(aup->phy_dev);
  823. }
  824. netif_start_queue(dev);
  825. if (au1000_debug > 4)
  826. printk("%s: open: Initialization done.\n", dev->name);
  827. return 0;
  828. }
  829. static int au1000_close(struct net_device *dev)
  830. {
  831. unsigned long flags;
  832. struct au1000_private *const aup = (struct au1000_private *) dev->priv;
  833. if (au1000_debug > 4)
  834. printk("%s: close: dev=%p\n", dev->name, dev);
  835. if (aup->phy_dev)
  836. phy_stop(aup->phy_dev);
  837. spin_lock_irqsave(&aup->lock, flags);
  838. reset_mac_unlocked (dev);
  839. /* stop the device */
  840. netif_stop_queue(dev);
  841. /* disable the interrupt */
  842. free_irq(dev->irq, dev);
  843. spin_unlock_irqrestore(&aup->lock, flags);
  844. return 0;
  845. }
  846. static void __exit au1000_cleanup_module(void)
  847. {
  848. int i, j;
  849. struct net_device *dev;
  850. struct au1000_private *aup;
  851. for (i = 0; i < num_ifs; i++) {
  852. dev = iflist[i].dev;
  853. if (dev) {
  854. aup = (struct au1000_private *) dev->priv;
  855. unregister_netdev(dev);
  856. for (j = 0; j < NUM_RX_DMA; j++)
  857. if (aup->rx_db_inuse[j])
  858. ReleaseDB(aup, aup->rx_db_inuse[j]);
  859. for (j = 0; j < NUM_TX_DMA; j++)
  860. if (aup->tx_db_inuse[j])
  861. ReleaseDB(aup, aup->tx_db_inuse[j]);
  862. dma_free_noncoherent(NULL, MAX_BUF_SIZE *
  863. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  864. (void *)aup->vaddr, aup->dma_addr);
  865. release_mem_region(dev->base_addr, MAC_IOSIZE);
  866. release_mem_region(CPHYSADDR(iflist[i].macen_addr), 4);
  867. free_netdev(dev);
  868. }
  869. }
  870. }
  871. static void update_tx_stats(struct net_device *dev, u32 status)
  872. {
  873. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  874. struct net_device_stats *ps = &aup->stats;
  875. if (status & TX_FRAME_ABORTED) {
  876. if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
  877. if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
  878. /* any other tx errors are only valid
  879. * in half duplex mode */
  880. ps->tx_errors++;
  881. ps->tx_aborted_errors++;
  882. }
  883. }
  884. else {
  885. ps->tx_errors++;
  886. ps->tx_aborted_errors++;
  887. if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
  888. ps->tx_carrier_errors++;
  889. }
  890. }
  891. }
  892. /*
  893. * Called from the interrupt service routine to acknowledge
  894. * the TX DONE bits. This is a must if the irq is setup as
  895. * edge triggered.
  896. */
  897. static void au1000_tx_ack(struct net_device *dev)
  898. {
  899. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  900. volatile tx_dma_t *ptxd;
  901. ptxd = aup->tx_dma_ring[aup->tx_tail];
  902. while (ptxd->buff_stat & TX_T_DONE) {
  903. update_tx_stats(dev, ptxd->status);
  904. ptxd->buff_stat &= ~TX_T_DONE;
  905. ptxd->len = 0;
  906. au_sync();
  907. aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
  908. ptxd = aup->tx_dma_ring[aup->tx_tail];
  909. if (aup->tx_full) {
  910. aup->tx_full = 0;
  911. netif_wake_queue(dev);
  912. }
  913. }
  914. }
  915. /*
  916. * Au1000 transmit routine.
  917. */
  918. static int au1000_tx(struct sk_buff *skb, struct net_device *dev)
  919. {
  920. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  921. struct net_device_stats *ps = &aup->stats;
  922. volatile tx_dma_t *ptxd;
  923. u32 buff_stat;
  924. db_dest_t *pDB;
  925. int i;
  926. if (au1000_debug > 5)
  927. printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
  928. dev->name, (unsigned)aup, skb->len,
  929. skb->data, aup->tx_head);
  930. ptxd = aup->tx_dma_ring[aup->tx_head];
  931. buff_stat = ptxd->buff_stat;
  932. if (buff_stat & TX_DMA_ENABLE) {
  933. /* We've wrapped around and the transmitter is still busy */
  934. netif_stop_queue(dev);
  935. aup->tx_full = 1;
  936. return 1;
  937. }
  938. else if (buff_stat & TX_T_DONE) {
  939. update_tx_stats(dev, ptxd->status);
  940. ptxd->len = 0;
  941. }
  942. if (aup->tx_full) {
  943. aup->tx_full = 0;
  944. netif_wake_queue(dev);
  945. }
  946. pDB = aup->tx_db_inuse[aup->tx_head];
  947. memcpy((void *)pDB->vaddr, skb->data, skb->len);
  948. if (skb->len < ETH_ZLEN) {
  949. for (i=skb->len; i<ETH_ZLEN; i++) {
  950. ((char *)pDB->vaddr)[i] = 0;
  951. }
  952. ptxd->len = ETH_ZLEN;
  953. }
  954. else
  955. ptxd->len = skb->len;
  956. ps->tx_packets++;
  957. ps->tx_bytes += ptxd->len;
  958. ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
  959. au_sync();
  960. dev_kfree_skb(skb);
  961. aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
  962. dev->trans_start = jiffies;
  963. return 0;
  964. }
  965. static inline void update_rx_stats(struct net_device *dev, u32 status)
  966. {
  967. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  968. struct net_device_stats *ps = &aup->stats;
  969. ps->rx_packets++;
  970. if (status & RX_MCAST_FRAME)
  971. ps->multicast++;
  972. if (status & RX_ERROR) {
  973. ps->rx_errors++;
  974. if (status & RX_MISSED_FRAME)
  975. ps->rx_missed_errors++;
  976. if (status & (RX_OVERLEN | RX_OVERLEN | RX_LEN_ERROR))
  977. ps->rx_length_errors++;
  978. if (status & RX_CRC_ERROR)
  979. ps->rx_crc_errors++;
  980. if (status & RX_COLL)
  981. ps->collisions++;
  982. }
  983. else
  984. ps->rx_bytes += status & RX_FRAME_LEN_MASK;
  985. }
  986. /*
  987. * Au1000 receive routine.
  988. */
  989. static int au1000_rx(struct net_device *dev)
  990. {
  991. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  992. struct sk_buff *skb;
  993. volatile rx_dma_t *prxd;
  994. u32 buff_stat, status;
  995. db_dest_t *pDB;
  996. u32 frmlen;
  997. if (au1000_debug > 5)
  998. printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head);
  999. prxd = aup->rx_dma_ring[aup->rx_head];
  1000. buff_stat = prxd->buff_stat;
  1001. while (buff_stat & RX_T_DONE) {
  1002. status = prxd->status;
  1003. pDB = aup->rx_db_inuse[aup->rx_head];
  1004. update_rx_stats(dev, status);
  1005. if (!(status & RX_ERROR)) {
  1006. /* good frame */
  1007. frmlen = (status & RX_FRAME_LEN_MASK);
  1008. frmlen -= 4; /* Remove FCS */
  1009. skb = dev_alloc_skb(frmlen + 2);
  1010. if (skb == NULL) {
  1011. printk(KERN_ERR
  1012. "%s: Memory squeeze, dropping packet.\n",
  1013. dev->name);
  1014. aup->stats.rx_dropped++;
  1015. continue;
  1016. }
  1017. skb->dev = dev;
  1018. skb_reserve(skb, 2); /* 16 byte IP header align */
  1019. eth_copy_and_sum(skb,
  1020. (unsigned char *)pDB->vaddr, frmlen, 0);
  1021. skb_put(skb, frmlen);
  1022. skb->protocol = eth_type_trans(skb, dev);
  1023. netif_rx(skb); /* pass the packet to upper layers */
  1024. }
  1025. else {
  1026. if (au1000_debug > 4) {
  1027. if (status & RX_MISSED_FRAME)
  1028. printk("rx miss\n");
  1029. if (status & RX_WDOG_TIMER)
  1030. printk("rx wdog\n");
  1031. if (status & RX_RUNT)
  1032. printk("rx runt\n");
  1033. if (status & RX_OVERLEN)
  1034. printk("rx overlen\n");
  1035. if (status & RX_COLL)
  1036. printk("rx coll\n");
  1037. if (status & RX_MII_ERROR)
  1038. printk("rx mii error\n");
  1039. if (status & RX_CRC_ERROR)
  1040. printk("rx crc error\n");
  1041. if (status & RX_LEN_ERROR)
  1042. printk("rx len error\n");
  1043. if (status & RX_U_CNTRL_FRAME)
  1044. printk("rx u control frame\n");
  1045. if (status & RX_MISSED_FRAME)
  1046. printk("rx miss\n");
  1047. }
  1048. }
  1049. prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
  1050. aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
  1051. au_sync();
  1052. /* next descriptor */
  1053. prxd = aup->rx_dma_ring[aup->rx_head];
  1054. buff_stat = prxd->buff_stat;
  1055. dev->last_rx = jiffies;
  1056. }
  1057. return 0;
  1058. }
  1059. /*
  1060. * Au1000 interrupt service routine.
  1061. */
  1062. static irqreturn_t au1000_interrupt(int irq, void *dev_id)
  1063. {
  1064. struct net_device *dev = (struct net_device *) dev_id;
  1065. if (dev == NULL) {
  1066. printk(KERN_ERR "%s: isr: null dev ptr\n", dev->name);
  1067. return IRQ_RETVAL(1);
  1068. }
  1069. /* Handle RX interrupts first to minimize chance of overrun */
  1070. au1000_rx(dev);
  1071. au1000_tx_ack(dev);
  1072. return IRQ_RETVAL(1);
  1073. }
  1074. /*
  1075. * The Tx ring has been full longer than the watchdog timeout
  1076. * value. The transmitter must be hung?
  1077. */
  1078. static void au1000_tx_timeout(struct net_device *dev)
  1079. {
  1080. printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
  1081. reset_mac(dev);
  1082. au1000_init(dev);
  1083. dev->trans_start = jiffies;
  1084. netif_wake_queue(dev);
  1085. }
  1086. static void set_rx_mode(struct net_device *dev)
  1087. {
  1088. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  1089. if (au1000_debug > 4)
  1090. printk("%s: set_rx_mode: flags=%x\n", dev->name, dev->flags);
  1091. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1092. aup->mac->control |= MAC_PROMISCUOUS;
  1093. } else if ((dev->flags & IFF_ALLMULTI) ||
  1094. dev->mc_count > MULTICAST_FILTER_LIMIT) {
  1095. aup->mac->control |= MAC_PASS_ALL_MULTI;
  1096. aup->mac->control &= ~MAC_PROMISCUOUS;
  1097. printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
  1098. } else {
  1099. int i;
  1100. struct dev_mc_list *mclist;
  1101. u32 mc_filter[2]; /* Multicast hash filter */
  1102. mc_filter[1] = mc_filter[0] = 0;
  1103. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1104. i++, mclist = mclist->next) {
  1105. set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26,
  1106. (long *)mc_filter);
  1107. }
  1108. aup->mac->multi_hash_high = mc_filter[1];
  1109. aup->mac->multi_hash_low = mc_filter[0];
  1110. aup->mac->control &= ~MAC_PROMISCUOUS;
  1111. aup->mac->control |= MAC_HASH_MODE;
  1112. }
  1113. }
  1114. static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1115. {
  1116. struct au1000_private *aup = (struct au1000_private *)dev->priv;
  1117. if (!netif_running(dev)) return -EINVAL;
  1118. if (!aup->phy_dev) return -EINVAL; // PHY not controllable
  1119. return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd);
  1120. }
  1121. static struct net_device_stats *au1000_get_stats(struct net_device *dev)
  1122. {
  1123. struct au1000_private *aup = (struct au1000_private *) dev->priv;
  1124. if (au1000_debug > 4)
  1125. printk("%s: au1000_get_stats: dev=%p\n", dev->name, dev);
  1126. if (netif_device_present(dev)) {
  1127. return &aup->stats;
  1128. }
  1129. return 0;
  1130. }
  1131. module_init(au1000_init_module);
  1132. module_exit(au1000_cleanup_module);