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