au1000_eth.c 33 KB

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