au1000_eth.c 33 KB

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