au1000_eth.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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_OVERLEN | 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. if (status & RX_MISSED_FRAME)
  669. printk("rx miss\n");
  670. }
  671. }
  672. prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
  673. aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
  674. au_sync();
  675. /* next descriptor */
  676. prxd = aup->rx_dma_ring[aup->rx_head];
  677. buff_stat = prxd->buff_stat;
  678. }
  679. return 0;
  680. }
  681. static void update_tx_stats(struct net_device *dev, u32 status)
  682. {
  683. struct au1000_private *aup = netdev_priv(dev);
  684. struct net_device_stats *ps = &dev->stats;
  685. if (status & TX_FRAME_ABORTED) {
  686. if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
  687. if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
  688. /* any other tx errors are only valid
  689. * in half duplex mode */
  690. ps->tx_errors++;
  691. ps->tx_aborted_errors++;
  692. }
  693. }
  694. else {
  695. ps->tx_errors++;
  696. ps->tx_aborted_errors++;
  697. if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
  698. ps->tx_carrier_errors++;
  699. }
  700. }
  701. }
  702. /*
  703. * Called from the interrupt service routine to acknowledge
  704. * the TX DONE bits. This is a must if the irq is setup as
  705. * edge triggered.
  706. */
  707. static void au1000_tx_ack(struct net_device *dev)
  708. {
  709. struct au1000_private *aup = netdev_priv(dev);
  710. volatile tx_dma_t *ptxd;
  711. ptxd = aup->tx_dma_ring[aup->tx_tail];
  712. while (ptxd->buff_stat & TX_T_DONE) {
  713. update_tx_stats(dev, ptxd->status);
  714. ptxd->buff_stat &= ~TX_T_DONE;
  715. ptxd->len = 0;
  716. au_sync();
  717. aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
  718. ptxd = aup->tx_dma_ring[aup->tx_tail];
  719. if (aup->tx_full) {
  720. aup->tx_full = 0;
  721. netif_wake_queue(dev);
  722. }
  723. }
  724. }
  725. /*
  726. * Au1000 interrupt service routine.
  727. */
  728. static irqreturn_t au1000_interrupt(int irq, void *dev_id)
  729. {
  730. struct net_device *dev = dev_id;
  731. /* Handle RX interrupts first to minimize chance of overrun */
  732. au1000_rx(dev);
  733. au1000_tx_ack(dev);
  734. return IRQ_RETVAL(1);
  735. }
  736. static int au1000_open(struct net_device *dev)
  737. {
  738. int retval;
  739. struct au1000_private *aup = netdev_priv(dev);
  740. if (au1000_debug > 4)
  741. printk("%s: open: dev=%p\n", dev->name, dev);
  742. if ((retval = request_irq(dev->irq, &au1000_interrupt, 0,
  743. dev->name, dev))) {
  744. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  745. dev->name, dev->irq);
  746. return retval;
  747. }
  748. if ((retval = au1000_init(dev))) {
  749. printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
  750. free_irq(dev->irq, dev);
  751. return retval;
  752. }
  753. if (aup->phy_dev) {
  754. /* cause the PHY state machine to schedule a link state check */
  755. aup->phy_dev->state = PHY_CHANGELINK;
  756. phy_start(aup->phy_dev);
  757. }
  758. netif_start_queue(dev);
  759. if (au1000_debug > 4)
  760. printk("%s: open: Initialization done.\n", dev->name);
  761. return 0;
  762. }
  763. static int au1000_close(struct net_device *dev)
  764. {
  765. unsigned long flags;
  766. struct au1000_private *const aup = netdev_priv(dev);
  767. if (au1000_debug > 4)
  768. printk("%s: close: dev=%p\n", dev->name, dev);
  769. if (aup->phy_dev)
  770. phy_stop(aup->phy_dev);
  771. spin_lock_irqsave(&aup->lock, flags);
  772. reset_mac_unlocked (dev);
  773. /* stop the device */
  774. netif_stop_queue(dev);
  775. /* disable the interrupt */
  776. free_irq(dev->irq, dev);
  777. spin_unlock_irqrestore(&aup->lock, flags);
  778. return 0;
  779. }
  780. /*
  781. * Au1000 transmit routine.
  782. */
  783. static int au1000_tx(struct sk_buff *skb, struct net_device *dev)
  784. {
  785. struct au1000_private *aup = netdev_priv(dev);
  786. struct net_device_stats *ps = &dev->stats;
  787. volatile tx_dma_t *ptxd;
  788. u32 buff_stat;
  789. db_dest_t *pDB;
  790. int i;
  791. if (au1000_debug > 5)
  792. printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
  793. dev->name, (unsigned)aup, skb->len,
  794. skb->data, aup->tx_head);
  795. ptxd = aup->tx_dma_ring[aup->tx_head];
  796. buff_stat = ptxd->buff_stat;
  797. if (buff_stat & TX_DMA_ENABLE) {
  798. /* We've wrapped around and the transmitter is still busy */
  799. netif_stop_queue(dev);
  800. aup->tx_full = 1;
  801. return 1;
  802. }
  803. else if (buff_stat & TX_T_DONE) {
  804. update_tx_stats(dev, ptxd->status);
  805. ptxd->len = 0;
  806. }
  807. if (aup->tx_full) {
  808. aup->tx_full = 0;
  809. netif_wake_queue(dev);
  810. }
  811. pDB = aup->tx_db_inuse[aup->tx_head];
  812. skb_copy_from_linear_data(skb, pDB->vaddr, skb->len);
  813. if (skb->len < ETH_ZLEN) {
  814. for (i=skb->len; i<ETH_ZLEN; i++) {
  815. ((char *)pDB->vaddr)[i] = 0;
  816. }
  817. ptxd->len = ETH_ZLEN;
  818. }
  819. else
  820. ptxd->len = skb->len;
  821. ps->tx_packets++;
  822. ps->tx_bytes += ptxd->len;
  823. ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
  824. au_sync();
  825. dev_kfree_skb(skb);
  826. aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
  827. dev->trans_start = jiffies;
  828. return 0;
  829. }
  830. /*
  831. * The Tx ring has been full longer than the watchdog timeout
  832. * value. The transmitter must be hung?
  833. */
  834. static void au1000_tx_timeout(struct net_device *dev)
  835. {
  836. printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
  837. reset_mac(dev);
  838. au1000_init(dev);
  839. dev->trans_start = jiffies;
  840. netif_wake_queue(dev);
  841. }
  842. static void au1000_multicast_list(struct net_device *dev)
  843. {
  844. struct au1000_private *aup = netdev_priv(dev);
  845. if (au1000_debug > 4)
  846. printk("%s: au1000_multicast_list: flags=%x\n", dev->name, dev->flags);
  847. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  848. aup->mac->control |= MAC_PROMISCUOUS;
  849. } else if ((dev->flags & IFF_ALLMULTI) ||
  850. dev->mc_count > MULTICAST_FILTER_LIMIT) {
  851. aup->mac->control |= MAC_PASS_ALL_MULTI;
  852. aup->mac->control &= ~MAC_PROMISCUOUS;
  853. printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
  854. } else {
  855. int i;
  856. struct dev_mc_list *mclist;
  857. u32 mc_filter[2]; /* Multicast hash filter */
  858. mc_filter[1] = mc_filter[0] = 0;
  859. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  860. i++, mclist = mclist->next) {
  861. set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26,
  862. (long *)mc_filter);
  863. }
  864. aup->mac->multi_hash_high = mc_filter[1];
  865. aup->mac->multi_hash_low = mc_filter[0];
  866. aup->mac->control &= ~MAC_PROMISCUOUS;
  867. aup->mac->control |= MAC_HASH_MODE;
  868. }
  869. }
  870. static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  871. {
  872. struct au1000_private *aup = netdev_priv(dev);
  873. if (!netif_running(dev)) return -EINVAL;
  874. if (!aup->phy_dev) return -EINVAL; // PHY not controllable
  875. return phy_mii_ioctl(aup->phy_dev, if_mii(rq), cmd);
  876. }
  877. static const struct net_device_ops au1000_netdev_ops = {
  878. .ndo_open = au1000_open,
  879. .ndo_stop = au1000_close,
  880. .ndo_start_xmit = au1000_tx,
  881. .ndo_set_multicast_list = au1000_multicast_list,
  882. .ndo_do_ioctl = au1000_ioctl,
  883. .ndo_tx_timeout = au1000_tx_timeout,
  884. .ndo_set_mac_address = eth_mac_addr,
  885. .ndo_validate_addr = eth_validate_addr,
  886. .ndo_change_mtu = eth_change_mtu,
  887. };
  888. static struct net_device * au1000_probe(int port_num)
  889. {
  890. static unsigned version_printed = 0;
  891. struct au1000_private *aup = NULL;
  892. struct net_device *dev = NULL;
  893. db_dest_t *pDB, *pDBfree;
  894. char ethaddr[6];
  895. int irq, i, err;
  896. u32 base, macen;
  897. if (port_num >= NUM_ETH_INTERFACES)
  898. return NULL;
  899. base = CPHYSADDR(iflist[port_num].base_addr );
  900. macen = CPHYSADDR(iflist[port_num].macen_addr);
  901. irq = iflist[port_num].irq;
  902. if (!request_mem_region( base, MAC_IOSIZE, "Au1x00 ENET") ||
  903. !request_mem_region(macen, 4, "Au1x00 ENET"))
  904. return NULL;
  905. if (version_printed++ == 0)
  906. printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
  907. dev = alloc_etherdev(sizeof(struct au1000_private));
  908. if (!dev) {
  909. printk(KERN_ERR "%s: alloc_etherdev failed\n", DRV_NAME);
  910. return NULL;
  911. }
  912. if ((err = register_netdev(dev)) != 0) {
  913. printk(KERN_ERR "%s: Cannot register net device, error %d\n",
  914. DRV_NAME, err);
  915. free_netdev(dev);
  916. return NULL;
  917. }
  918. printk("%s: Au1xx0 Ethernet found at 0x%x, irq %d\n",
  919. dev->name, base, irq);
  920. aup = netdev_priv(dev);
  921. spin_lock_init(&aup->lock);
  922. /* Allocate the data buffers */
  923. /* Snooping works fine with eth on all au1xxx */
  924. aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
  925. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  926. &aup->dma_addr, 0);
  927. if (!aup->vaddr) {
  928. free_netdev(dev);
  929. release_mem_region( base, MAC_IOSIZE);
  930. release_mem_region(macen, 4);
  931. return NULL;
  932. }
  933. /* aup->mac is the base address of the MAC's registers */
  934. aup->mac = (volatile mac_reg_t *)iflist[port_num].base_addr;
  935. /* Setup some variables for quick register address access */
  936. aup->enable = (volatile u32 *)iflist[port_num].macen_addr;
  937. aup->mac_id = port_num;
  938. au_macs[port_num] = aup;
  939. if (port_num == 0) {
  940. if (prom_get_ethernet_addr(ethaddr) == 0)
  941. memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
  942. else {
  943. printk(KERN_INFO "%s: No MAC address found\n",
  944. dev->name);
  945. /* Use the hard coded MAC addresses */
  946. }
  947. setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
  948. } else if (port_num == 1)
  949. setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
  950. /*
  951. * Assign to the Ethernet ports two consecutive MAC addresses
  952. * to match those that are printed on their stickers
  953. */
  954. memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
  955. dev->dev_addr[5] += port_num;
  956. *aup->enable = 0;
  957. aup->mac_enabled = 0;
  958. aup->mii_bus = mdiobus_alloc();
  959. if (aup->mii_bus == NULL)
  960. goto err_out;
  961. aup->mii_bus->priv = dev;
  962. aup->mii_bus->read = au1000_mdiobus_read;
  963. aup->mii_bus->write = au1000_mdiobus_write;
  964. aup->mii_bus->reset = au1000_mdiobus_reset;
  965. aup->mii_bus->name = "au1000_eth_mii";
  966. snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%x", aup->mac_id);
  967. aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  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);