au1000_eth.c 33 KB

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