au1000_eth.c 34 KB

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