au1000_eth.c 33 KB

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