au1000_eth.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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 = netdev_priv(dev);
  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 = netdev_priv(dev);
  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 = netdev_priv(dev);
  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->attached_dev);
  312. phydev = phy_connect(dev, phydev->dev.bus_id, &au1000_adjust_link, 0,
  313. PHY_INTERFACE_MODE_MII);
  314. if (IS_ERR(phydev)) {
  315. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  316. return PTR_ERR(phydev);
  317. }
  318. /* mask with MAC supported features */
  319. phydev->supported &= (SUPPORTED_10baseT_Half
  320. | SUPPORTED_10baseT_Full
  321. | SUPPORTED_100baseT_Half
  322. | SUPPORTED_100baseT_Full
  323. | SUPPORTED_Autoneg
  324. /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
  325. | SUPPORTED_MII
  326. | SUPPORTED_TP);
  327. phydev->advertising = phydev->supported;
  328. aup->old_link = 0;
  329. aup->old_speed = 0;
  330. aup->old_duplex = -1;
  331. aup->phy_dev = phydev;
  332. printk(KERN_INFO "%s: attached PHY driver [%s] "
  333. "(mii_bus:phy_addr=%s, irq=%d)\n",
  334. dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
  335. return 0;
  336. }
  337. /*
  338. * Buffer allocation/deallocation routines. The buffer descriptor returned
  339. * has the virtual and dma address of a buffer suitable for
  340. * both, receive and transmit operations.
  341. */
  342. static db_dest_t *GetFreeDB(struct au1000_private *aup)
  343. {
  344. db_dest_t *pDB;
  345. pDB = aup->pDBfree;
  346. if (pDB) {
  347. aup->pDBfree = pDB->pnext;
  348. }
  349. return pDB;
  350. }
  351. void ReleaseDB(struct au1000_private *aup, db_dest_t *pDB)
  352. {
  353. db_dest_t *pDBfree = aup->pDBfree;
  354. if (pDBfree)
  355. pDBfree->pnext = pDB;
  356. aup->pDBfree = pDB;
  357. }
  358. static void enable_rx_tx(struct net_device *dev)
  359. {
  360. struct au1000_private *aup = netdev_priv(dev);
  361. if (au1000_debug > 4)
  362. printk(KERN_INFO "%s: enable_rx_tx\n", dev->name);
  363. aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
  364. au_sync_delay(10);
  365. }
  366. static void hard_stop(struct net_device *dev)
  367. {
  368. struct au1000_private *aup = netdev_priv(dev);
  369. if (au1000_debug > 4)
  370. printk(KERN_INFO "%s: hard stop\n", dev->name);
  371. aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
  372. au_sync_delay(10);
  373. }
  374. static void enable_mac(struct net_device *dev, int force_reset)
  375. {
  376. unsigned long flags;
  377. struct au1000_private *aup = netdev_priv(dev);
  378. spin_lock_irqsave(&aup->lock, flags);
  379. if(force_reset || (!aup->mac_enabled)) {
  380. *aup->enable = MAC_EN_CLOCK_ENABLE;
  381. au_sync_delay(2);
  382. *aup->enable = (MAC_EN_RESET0 | MAC_EN_RESET1 | MAC_EN_RESET2
  383. | MAC_EN_CLOCK_ENABLE);
  384. au_sync_delay(2);
  385. aup->mac_enabled = 1;
  386. }
  387. spin_unlock_irqrestore(&aup->lock, flags);
  388. }
  389. static void reset_mac_unlocked(struct net_device *dev)
  390. {
  391. struct au1000_private *const aup = netdev_priv(dev);
  392. int i;
  393. hard_stop(dev);
  394. *aup->enable = MAC_EN_CLOCK_ENABLE;
  395. au_sync_delay(2);
  396. *aup->enable = 0;
  397. au_sync_delay(2);
  398. aup->tx_full = 0;
  399. for (i = 0; i < NUM_RX_DMA; i++) {
  400. /* reset control bits */
  401. aup->rx_dma_ring[i]->buff_stat &= ~0xf;
  402. }
  403. for (i = 0; i < NUM_TX_DMA; i++) {
  404. /* reset control bits */
  405. aup->tx_dma_ring[i]->buff_stat &= ~0xf;
  406. }
  407. aup->mac_enabled = 0;
  408. }
  409. static void reset_mac(struct net_device *dev)
  410. {
  411. struct au1000_private *const aup = netdev_priv(dev);
  412. unsigned long flags;
  413. if (au1000_debug > 4)
  414. printk(KERN_INFO "%s: reset mac, aup %x\n",
  415. dev->name, (unsigned)aup);
  416. spin_lock_irqsave(&aup->lock, flags);
  417. reset_mac_unlocked (dev);
  418. spin_unlock_irqrestore(&aup->lock, flags);
  419. }
  420. /*
  421. * Setup the receive and transmit "rings". These pointers are the addresses
  422. * of the rx and tx MAC DMA registers so they are fixed by the hardware --
  423. * these are not descriptors sitting in memory.
  424. */
  425. static void
  426. setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
  427. {
  428. int i;
  429. for (i = 0; i < NUM_RX_DMA; i++) {
  430. aup->rx_dma_ring[i] =
  431. (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i);
  432. }
  433. for (i = 0; i < NUM_TX_DMA; i++) {
  434. aup->tx_dma_ring[i] =
  435. (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i);
  436. }
  437. }
  438. static struct {
  439. u32 base_addr;
  440. u32 macen_addr;
  441. int irq;
  442. struct net_device *dev;
  443. } iflist[2] = {
  444. #ifdef CONFIG_SOC_AU1000
  445. {AU1000_ETH0_BASE, AU1000_MAC0_ENABLE, AU1000_MAC0_DMA_INT},
  446. {AU1000_ETH1_BASE, AU1000_MAC1_ENABLE, AU1000_MAC1_DMA_INT}
  447. #endif
  448. #ifdef CONFIG_SOC_AU1100
  449. {AU1100_ETH0_BASE, AU1100_MAC0_ENABLE, AU1100_MAC0_DMA_INT}
  450. #endif
  451. #ifdef CONFIG_SOC_AU1500
  452. {AU1500_ETH0_BASE, AU1500_MAC0_ENABLE, AU1500_MAC0_DMA_INT},
  453. {AU1500_ETH1_BASE, AU1500_MAC1_ENABLE, AU1500_MAC1_DMA_INT}
  454. #endif
  455. #ifdef CONFIG_SOC_AU1550
  456. {AU1550_ETH0_BASE, AU1550_MAC0_ENABLE, AU1550_MAC0_DMA_INT},
  457. {AU1550_ETH1_BASE, AU1550_MAC1_ENABLE, AU1550_MAC1_DMA_INT}
  458. #endif
  459. };
  460. static int num_ifs;
  461. /*
  462. * Setup the base address and interrupt of the Au1xxx ethernet macs
  463. * based on cpu type and whether the interface is enabled in sys_pinfunc
  464. * register. The last interface is enabled if SYS_PF_NI2 (bit 4) is 0.
  465. */
  466. static int __init au1000_init_module(void)
  467. {
  468. int ni = (int)((au_readl(SYS_PINFUNC) & (u32)(SYS_PF_NI2)) >> 4);
  469. struct net_device *dev;
  470. int i, found_one = 0;
  471. num_ifs = NUM_ETH_INTERFACES - ni;
  472. for(i = 0; i < num_ifs; i++) {
  473. dev = au1000_probe(i);
  474. iflist[i].dev = dev;
  475. if (dev)
  476. found_one++;
  477. }
  478. if (!found_one)
  479. return -ENODEV;
  480. return 0;
  481. }
  482. /*
  483. * ethtool operations
  484. */
  485. static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  486. {
  487. struct au1000_private *aup = netdev_priv(dev);
  488. if (aup->phy_dev)
  489. return phy_ethtool_gset(aup->phy_dev, cmd);
  490. return -EINVAL;
  491. }
  492. static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  493. {
  494. struct au1000_private *aup = netdev_priv(dev);
  495. if (!capable(CAP_NET_ADMIN))
  496. return -EPERM;
  497. if (aup->phy_dev)
  498. return phy_ethtool_sset(aup->phy_dev, cmd);
  499. return -EINVAL;
  500. }
  501. static void
  502. au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  503. {
  504. struct au1000_private *aup = netdev_priv(dev);
  505. strcpy(info->driver, DRV_NAME);
  506. strcpy(info->version, DRV_VERSION);
  507. info->fw_version[0] = '\0';
  508. sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
  509. info->regdump_len = 0;
  510. }
  511. static const struct ethtool_ops au1000_ethtool_ops = {
  512. .get_settings = au1000_get_settings,
  513. .set_settings = au1000_set_settings,
  514. .get_drvinfo = au1000_get_drvinfo,
  515. .get_link = ethtool_op_get_link,
  516. };
  517. static struct net_device * au1000_probe(int port_num)
  518. {
  519. static unsigned version_printed = 0;
  520. struct au1000_private *aup = NULL;
  521. struct net_device *dev = NULL;
  522. db_dest_t *pDB, *pDBfree;
  523. char ethaddr[6];
  524. int irq, i, err;
  525. u32 base, macen;
  526. if (port_num >= NUM_ETH_INTERFACES)
  527. return NULL;
  528. base = CPHYSADDR(iflist[port_num].base_addr );
  529. macen = CPHYSADDR(iflist[port_num].macen_addr);
  530. irq = iflist[port_num].irq;
  531. if (!request_mem_region( base, MAC_IOSIZE, "Au1x00 ENET") ||
  532. !request_mem_region(macen, 4, "Au1x00 ENET"))
  533. return NULL;
  534. if (version_printed++ == 0)
  535. printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
  536. dev = alloc_etherdev(sizeof(struct au1000_private));
  537. if (!dev) {
  538. printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME);
  539. return NULL;
  540. }
  541. if ((err = register_netdev(dev)) != 0) {
  542. printk(KERN_ERR "%s: Cannot register net device, error %d\n",
  543. DRV_NAME, err);
  544. free_netdev(dev);
  545. return NULL;
  546. }
  547. printk("%s: Au1xx0 Ethernet found at 0x%x, irq %d\n",
  548. dev->name, base, irq);
  549. aup = netdev_priv(dev);
  550. spin_lock_init(&aup->lock);
  551. /* Allocate the data buffers */
  552. /* Snooping works fine with eth on all au1xxx */
  553. aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
  554. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  555. &aup->dma_addr, 0);
  556. if (!aup->vaddr) {
  557. free_netdev(dev);
  558. release_mem_region( base, MAC_IOSIZE);
  559. release_mem_region(macen, 4);
  560. return NULL;
  561. }
  562. /* aup->mac is the base address of the MAC's registers */
  563. aup->mac = (volatile mac_reg_t *)iflist[port_num].base_addr;
  564. /* Setup some variables for quick register address access */
  565. aup->enable = (volatile u32 *)iflist[port_num].macen_addr;
  566. aup->mac_id = port_num;
  567. au_macs[port_num] = aup;
  568. if (port_num == 0) {
  569. if (prom_get_ethernet_addr(ethaddr) == 0)
  570. memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
  571. else {
  572. printk(KERN_INFO "%s: No MAC address found\n",
  573. dev->name);
  574. /* Use the hard coded MAC addresses */
  575. }
  576. setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
  577. } else if (port_num == 1)
  578. setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
  579. /*
  580. * Assign to the Ethernet ports two consecutive MAC addresses
  581. * to match those that are printed on their stickers
  582. */
  583. memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
  584. dev->dev_addr[5] += port_num;
  585. *aup->enable = 0;
  586. aup->mac_enabled = 0;
  587. aup->mii_bus = mdiobus_alloc();
  588. if (aup->mii_bus == NULL)
  589. goto err_out;
  590. aup->mii_bus->priv = dev;
  591. aup->mii_bus->read = au1000_mdiobus_read;
  592. aup->mii_bus->write = au1000_mdiobus_write;
  593. aup->mii_bus->reset = au1000_mdiobus_reset;
  594. aup->mii_bus->name = "au1000_eth_mii";
  595. snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%x", aup->mac_id);
  596. aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  597. for(i = 0; i < PHY_MAX_ADDR; ++i)
  598. aup->mii_bus->irq[i] = PHY_POLL;
  599. /* if known, set corresponding PHY IRQs */
  600. #if defined(AU1XXX_PHY_STATIC_CONFIG)
  601. # if defined(AU1XXX_PHY0_IRQ)
  602. if (AU1XXX_PHY0_BUSID == aup->mac_id)
  603. aup->mii_bus->irq[AU1XXX_PHY0_ADDR] = AU1XXX_PHY0_IRQ;
  604. # endif
  605. # if defined(AU1XXX_PHY1_IRQ)
  606. if (AU1XXX_PHY1_BUSID == aup->mac_id)
  607. aup->mii_bus->irq[AU1XXX_PHY1_ADDR] = AU1XXX_PHY1_IRQ;
  608. # endif
  609. #endif
  610. mdiobus_register(aup->mii_bus);
  611. if (mii_probe(dev) != 0) {
  612. goto err_out;
  613. }
  614. pDBfree = NULL;
  615. /* setup the data buffer descriptors and attach a buffer to each one */
  616. pDB = aup->db;
  617. for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
  618. pDB->pnext = pDBfree;
  619. pDBfree = pDB;
  620. pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
  621. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  622. pDB++;
  623. }
  624. aup->pDBfree = pDBfree;
  625. for (i = 0; i < NUM_RX_DMA; i++) {
  626. pDB = GetFreeDB(aup);
  627. if (!pDB) {
  628. goto err_out;
  629. }
  630. aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  631. aup->rx_db_inuse[i] = pDB;
  632. }
  633. for (i = 0; i < NUM_TX_DMA; i++) {
  634. pDB = GetFreeDB(aup);
  635. if (!pDB) {
  636. goto err_out;
  637. }
  638. aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  639. aup->tx_dma_ring[i]->len = 0;
  640. aup->tx_db_inuse[i] = pDB;
  641. }
  642. dev->base_addr = base;
  643. dev->irq = irq;
  644. dev->open = au1000_open;
  645. dev->hard_start_xmit = au1000_tx;
  646. dev->stop = au1000_close;
  647. dev->set_multicast_list = &set_rx_mode;
  648. dev->do_ioctl = &au1000_ioctl;
  649. SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
  650. dev->tx_timeout = au1000_tx_timeout;
  651. dev->watchdog_timeo = ETH_TX_TIMEOUT;
  652. /*
  653. * The boot code uses the ethernet controller, so reset it to start
  654. * fresh. au1000_init() expects that the device is in reset state.
  655. */
  656. reset_mac(dev);
  657. return dev;
  658. err_out:
  659. if (aup->mii_bus != NULL) {
  660. mdiobus_unregister(aup->mii_bus);
  661. mdiobus_free(aup->mii_bus);
  662. }
  663. /* here we should have a valid dev plus aup-> register addresses
  664. * so we can reset the mac properly.*/
  665. reset_mac(dev);
  666. for (i = 0; i < NUM_RX_DMA; i++) {
  667. if (aup->rx_db_inuse[i])
  668. ReleaseDB(aup, aup->rx_db_inuse[i]);
  669. }
  670. for (i = 0; i < NUM_TX_DMA; i++) {
  671. if (aup->tx_db_inuse[i])
  672. ReleaseDB(aup, aup->tx_db_inuse[i]);
  673. }
  674. dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
  675. (void *)aup->vaddr, aup->dma_addr);
  676. unregister_netdev(dev);
  677. free_netdev(dev);
  678. release_mem_region( base, MAC_IOSIZE);
  679. release_mem_region(macen, 4);
  680. return NULL;
  681. }
  682. /*
  683. * Initialize the interface.
  684. *
  685. * When the device powers up, the clocks are disabled and the
  686. * mac is in reset state. When the interface is closed, we
  687. * do the same -- reset the device and disable the clocks to
  688. * conserve power. Thus, whenever au1000_init() is called,
  689. * the device should already be in reset state.
  690. */
  691. static int au1000_init(struct net_device *dev)
  692. {
  693. struct au1000_private *aup = netdev_priv(dev);
  694. unsigned long flags;
  695. int i;
  696. u32 control;
  697. if (au1000_debug > 4)
  698. printk("%s: au1000_init\n", dev->name);
  699. /* bring the device out of reset */
  700. enable_mac(dev, 1);
  701. spin_lock_irqsave(&aup->lock, flags);
  702. aup->mac->control = 0;
  703. aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
  704. aup->tx_tail = aup->tx_head;
  705. aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
  706. aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
  707. aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
  708. dev->dev_addr[1]<<8 | dev->dev_addr[0];
  709. for (i = 0; i < NUM_RX_DMA; i++) {
  710. aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
  711. }
  712. au_sync();
  713. control = MAC_RX_ENABLE | MAC_TX_ENABLE;
  714. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  715. control |= MAC_BIG_ENDIAN;
  716. #endif
  717. if (aup->phy_dev) {
  718. if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
  719. control |= MAC_FULL_DUPLEX;
  720. else
  721. control |= MAC_DISABLE_RX_OWN;
  722. } else { /* PHY-less op, assume full-duplex */
  723. control |= MAC_FULL_DUPLEX;
  724. }
  725. aup->mac->control = control;
  726. aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
  727. au_sync();
  728. spin_unlock_irqrestore(&aup->lock, flags);
  729. return 0;
  730. }
  731. static void
  732. au1000_adjust_link(struct net_device *dev)
  733. {
  734. struct au1000_private *aup = netdev_priv(dev);
  735. struct phy_device *phydev = aup->phy_dev;
  736. unsigned long flags;
  737. int status_change = 0;
  738. BUG_ON(!aup->phy_dev);
  739. spin_lock_irqsave(&aup->lock, flags);
  740. if (phydev->link && (aup->old_speed != phydev->speed)) {
  741. // speed changed
  742. switch(phydev->speed) {
  743. case SPEED_10:
  744. case SPEED_100:
  745. break;
  746. default:
  747. printk(KERN_WARNING
  748. "%s: Speed (%d) is not 10/100 ???\n",
  749. dev->name, phydev->speed);
  750. break;
  751. }
  752. aup->old_speed = phydev->speed;
  753. status_change = 1;
  754. }
  755. if (phydev->link && (aup->old_duplex != phydev->duplex)) {
  756. // duplex mode changed
  757. /* switching duplex mode requires to disable rx and tx! */
  758. hard_stop(dev);
  759. if (DUPLEX_FULL == phydev->duplex)
  760. aup->mac->control = ((aup->mac->control
  761. | MAC_FULL_DUPLEX)
  762. & ~MAC_DISABLE_RX_OWN);
  763. else
  764. aup->mac->control = ((aup->mac->control
  765. & ~MAC_FULL_DUPLEX)
  766. | MAC_DISABLE_RX_OWN);
  767. au_sync_delay(1);
  768. enable_rx_tx(dev);
  769. aup->old_duplex = phydev->duplex;
  770. status_change = 1;
  771. }
  772. if(phydev->link != aup->old_link) {
  773. // link state changed
  774. if (!phydev->link) {
  775. /* link went down */
  776. aup->old_speed = 0;
  777. aup->old_duplex = -1;
  778. }
  779. aup->old_link = phydev->link;
  780. status_change = 1;
  781. }
  782. spin_unlock_irqrestore(&aup->lock, flags);
  783. if (status_change) {
  784. if (phydev->link)
  785. printk(KERN_INFO "%s: link up (%d/%s)\n",
  786. dev->name, phydev->speed,
  787. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  788. else
  789. printk(KERN_INFO "%s: link down\n", dev->name);
  790. }
  791. }
  792. static int au1000_open(struct net_device *dev)
  793. {
  794. int retval;
  795. struct au1000_private *aup = netdev_priv(dev);
  796. if (au1000_debug > 4)
  797. printk("%s: open: dev=%p\n", dev->name, dev);
  798. if ((retval = request_irq(dev->irq, &au1000_interrupt, 0,
  799. dev->name, dev))) {
  800. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  801. dev->name, dev->irq);
  802. return retval;
  803. }
  804. if ((retval = au1000_init(dev))) {
  805. printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
  806. free_irq(dev->irq, dev);
  807. return retval;
  808. }
  809. if (aup->phy_dev) {
  810. /* cause the PHY state machine to schedule a link state check */
  811. aup->phy_dev->state = PHY_CHANGELINK;
  812. phy_start(aup->phy_dev);
  813. }
  814. netif_start_queue(dev);
  815. if (au1000_debug > 4)
  816. printk("%s: open: Initialization done.\n", dev->name);
  817. return 0;
  818. }
  819. static int au1000_close(struct net_device *dev)
  820. {
  821. unsigned long flags;
  822. struct au1000_private *const aup = netdev_priv(dev);
  823. if (au1000_debug > 4)
  824. printk("%s: close: dev=%p\n", dev->name, dev);
  825. if (aup->phy_dev)
  826. phy_stop(aup->phy_dev);
  827. spin_lock_irqsave(&aup->lock, flags);
  828. reset_mac_unlocked (dev);
  829. /* stop the device */
  830. netif_stop_queue(dev);
  831. /* disable the interrupt */
  832. free_irq(dev->irq, dev);
  833. spin_unlock_irqrestore(&aup->lock, flags);
  834. return 0;
  835. }
  836. static void __exit au1000_cleanup_module(void)
  837. {
  838. int i, j;
  839. struct net_device *dev;
  840. struct au1000_private *aup;
  841. for (i = 0; i < num_ifs; i++) {
  842. dev = iflist[i].dev;
  843. if (dev) {
  844. aup = netdev_priv(dev);
  845. unregister_netdev(dev);
  846. mdiobus_unregister(aup->mii_bus);
  847. mdiobus_free(aup->mii_bus);
  848. for (j = 0; j < NUM_RX_DMA; j++)
  849. if (aup->rx_db_inuse[j])
  850. ReleaseDB(aup, aup->rx_db_inuse[j]);
  851. for (j = 0; j < NUM_TX_DMA; j++)
  852. if (aup->tx_db_inuse[j])
  853. ReleaseDB(aup, aup->tx_db_inuse[j]);
  854. dma_free_noncoherent(NULL, MAX_BUF_SIZE *
  855. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  856. (void *)aup->vaddr, aup->dma_addr);
  857. release_mem_region(dev->base_addr, MAC_IOSIZE);
  858. release_mem_region(CPHYSADDR(iflist[i].macen_addr), 4);
  859. free_netdev(dev);
  860. }
  861. }
  862. }
  863. static void update_tx_stats(struct net_device *dev, u32 status)
  864. {
  865. struct au1000_private *aup = netdev_priv(dev);
  866. struct net_device_stats *ps = &dev->stats;
  867. if (status & TX_FRAME_ABORTED) {
  868. if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
  869. if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
  870. /* any other tx errors are only valid
  871. * in half duplex mode */
  872. ps->tx_errors++;
  873. ps->tx_aborted_errors++;
  874. }
  875. }
  876. else {
  877. ps->tx_errors++;
  878. ps->tx_aborted_errors++;
  879. if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
  880. ps->tx_carrier_errors++;
  881. }
  882. }
  883. }
  884. /*
  885. * Called from the interrupt service routine to acknowledge
  886. * the TX DONE bits. This is a must if the irq is setup as
  887. * edge triggered.
  888. */
  889. static void au1000_tx_ack(struct net_device *dev)
  890. {
  891. struct au1000_private *aup = netdev_priv(dev);
  892. volatile tx_dma_t *ptxd;
  893. ptxd = aup->tx_dma_ring[aup->tx_tail];
  894. while (ptxd->buff_stat & TX_T_DONE) {
  895. update_tx_stats(dev, ptxd->status);
  896. ptxd->buff_stat &= ~TX_T_DONE;
  897. ptxd->len = 0;
  898. au_sync();
  899. aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
  900. ptxd = aup->tx_dma_ring[aup->tx_tail];
  901. if (aup->tx_full) {
  902. aup->tx_full = 0;
  903. netif_wake_queue(dev);
  904. }
  905. }
  906. }
  907. /*
  908. * Au1000 transmit routine.
  909. */
  910. static int au1000_tx(struct sk_buff *skb, struct net_device *dev)
  911. {
  912. struct au1000_private *aup = netdev_priv(dev);
  913. struct net_device_stats *ps = &dev->stats;
  914. volatile tx_dma_t *ptxd;
  915. u32 buff_stat;
  916. db_dest_t *pDB;
  917. int i;
  918. if (au1000_debug > 5)
  919. printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
  920. dev->name, (unsigned)aup, skb->len,
  921. skb->data, aup->tx_head);
  922. ptxd = aup->tx_dma_ring[aup->tx_head];
  923. buff_stat = ptxd->buff_stat;
  924. if (buff_stat & TX_DMA_ENABLE) {
  925. /* We've wrapped around and the transmitter is still busy */
  926. netif_stop_queue(dev);
  927. aup->tx_full = 1;
  928. return 1;
  929. }
  930. else if (buff_stat & TX_T_DONE) {
  931. update_tx_stats(dev, ptxd->status);
  932. ptxd->len = 0;
  933. }
  934. if (aup->tx_full) {
  935. aup->tx_full = 0;
  936. netif_wake_queue(dev);
  937. }
  938. pDB = aup->tx_db_inuse[aup->tx_head];
  939. skb_copy_from_linear_data(skb, pDB->vaddr, skb->len);
  940. if (skb->len < ETH_ZLEN) {
  941. for (i=skb->len; i<ETH_ZLEN; i++) {
  942. ((char *)pDB->vaddr)[i] = 0;
  943. }
  944. ptxd->len = ETH_ZLEN;
  945. }
  946. else
  947. ptxd->len = skb->len;
  948. ps->tx_packets++;
  949. ps->tx_bytes += ptxd->len;
  950. ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
  951. au_sync();
  952. dev_kfree_skb(skb);
  953. aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
  954. dev->trans_start = jiffies;
  955. return 0;
  956. }
  957. static inline void update_rx_stats(struct net_device *dev, u32 status)
  958. {
  959. struct au1000_private *aup = netdev_priv(dev);
  960. struct net_device_stats *ps = &dev->stats;
  961. ps->rx_packets++;
  962. if (status & RX_MCAST_FRAME)
  963. ps->multicast++;
  964. if (status & RX_ERROR) {
  965. ps->rx_errors++;
  966. if (status & RX_MISSED_FRAME)
  967. ps->rx_missed_errors++;
  968. if (status & (RX_OVERLEN | RX_OVERLEN | RX_LEN_ERROR))
  969. ps->rx_length_errors++;
  970. if (status & RX_CRC_ERROR)
  971. ps->rx_crc_errors++;
  972. if (status & RX_COLL)
  973. ps->collisions++;
  974. }
  975. else
  976. ps->rx_bytes += status & RX_FRAME_LEN_MASK;
  977. }
  978. /*
  979. * Au1000 receive routine.
  980. */
  981. static int au1000_rx(struct net_device *dev)
  982. {
  983. struct au1000_private *aup = netdev_priv(dev);
  984. struct sk_buff *skb;
  985. volatile rx_dma_t *prxd;
  986. u32 buff_stat, status;
  987. db_dest_t *pDB;
  988. u32 frmlen;
  989. if (au1000_debug > 5)
  990. printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head);
  991. prxd = aup->rx_dma_ring[aup->rx_head];
  992. buff_stat = prxd->buff_stat;
  993. while (buff_stat & RX_T_DONE) {
  994. status = prxd->status;
  995. pDB = aup->rx_db_inuse[aup->rx_head];
  996. update_rx_stats(dev, status);
  997. if (!(status & RX_ERROR)) {
  998. /* good frame */
  999. frmlen = (status & RX_FRAME_LEN_MASK);
  1000. frmlen -= 4; /* Remove FCS */
  1001. skb = dev_alloc_skb(frmlen + 2);
  1002. if (skb == NULL) {
  1003. printk(KERN_ERR
  1004. "%s: Memory squeeze, dropping packet.\n",
  1005. dev->name);
  1006. dev->stats.rx_dropped++;
  1007. continue;
  1008. }
  1009. skb_reserve(skb, 2); /* 16 byte IP header align */
  1010. skb_copy_to_linear_data(skb,
  1011. (unsigned char *)pDB->vaddr, frmlen);
  1012. skb_put(skb, frmlen);
  1013. skb->protocol = eth_type_trans(skb, dev);
  1014. netif_rx(skb); /* pass the packet to upper layers */
  1015. }
  1016. else {
  1017. if (au1000_debug > 4) {
  1018. if (status & RX_MISSED_FRAME)
  1019. printk("rx miss\n");
  1020. if (status & RX_WDOG_TIMER)
  1021. printk("rx wdog\n");
  1022. if (status & RX_RUNT)
  1023. printk("rx runt\n");
  1024. if (status & RX_OVERLEN)
  1025. printk("rx overlen\n");
  1026. if (status & RX_COLL)
  1027. printk("rx coll\n");
  1028. if (status & RX_MII_ERROR)
  1029. printk("rx mii error\n");
  1030. if (status & RX_CRC_ERROR)
  1031. printk("rx crc error\n");
  1032. if (status & RX_LEN_ERROR)
  1033. printk("rx len error\n");
  1034. if (status & RX_U_CNTRL_FRAME)
  1035. printk("rx u control frame\n");
  1036. if (status & RX_MISSED_FRAME)
  1037. printk("rx miss\n");
  1038. }
  1039. }
  1040. prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
  1041. aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
  1042. au_sync();
  1043. /* next descriptor */
  1044. prxd = aup->rx_dma_ring[aup->rx_head];
  1045. buff_stat = prxd->buff_stat;
  1046. }
  1047. return 0;
  1048. }
  1049. /*
  1050. * Au1000 interrupt service routine.
  1051. */
  1052. static irqreturn_t au1000_interrupt(int irq, void *dev_id)
  1053. {
  1054. struct net_device *dev = dev_id;
  1055. /* Handle RX interrupts first to minimize chance of overrun */
  1056. au1000_rx(dev);
  1057. au1000_tx_ack(dev);
  1058. return IRQ_RETVAL(1);
  1059. }
  1060. /*
  1061. * The Tx ring has been full longer than the watchdog timeout
  1062. * value. The transmitter must be hung?
  1063. */
  1064. static void au1000_tx_timeout(struct net_device *dev)
  1065. {
  1066. printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
  1067. reset_mac(dev);
  1068. au1000_init(dev);
  1069. dev->trans_start = jiffies;
  1070. netif_wake_queue(dev);
  1071. }
  1072. static void set_rx_mode(struct net_device *dev)
  1073. {
  1074. struct au1000_private *aup = netdev_priv(dev);
  1075. if (au1000_debug > 4)
  1076. printk("%s: set_rx_mode: flags=%x\n", dev->name, dev->flags);
  1077. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1078. aup->mac->control |= MAC_PROMISCUOUS;
  1079. } else if ((dev->flags & IFF_ALLMULTI) ||
  1080. dev->mc_count > MULTICAST_FILTER_LIMIT) {
  1081. aup->mac->control |= MAC_PASS_ALL_MULTI;
  1082. aup->mac->control &= ~MAC_PROMISCUOUS;
  1083. printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
  1084. } else {
  1085. int i;
  1086. struct dev_mc_list *mclist;
  1087. u32 mc_filter[2]; /* Multicast hash filter */
  1088. mc_filter[1] = mc_filter[0] = 0;
  1089. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1090. i++, mclist = mclist->next) {
  1091. set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26,
  1092. (long *)mc_filter);
  1093. }
  1094. aup->mac->multi_hash_high = mc_filter[1];
  1095. aup->mac->multi_hash_low = mc_filter[0];
  1096. aup->mac->control &= ~MAC_PROMISCUOUS;
  1097. aup->mac->control |= MAC_HASH_MODE;
  1098. }
  1099. }
  1100. static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1101. {
  1102. struct au1000_private *aup = netdev_priv(dev);
  1103. if (!netif_running(dev)) return -EINVAL;
  1104. if (!aup->phy_dev) return -EINVAL; // PHY not controllable
  1105. return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd);
  1106. }
  1107. module_init(au1000_init_module);
  1108. module_exit(au1000_cleanup_module);