au1000_eth.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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, "%s-%x",
  978. pdev->name, aup->mac_id);
  979. aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  980. if (aup->mii_bus->irq == NULL)
  981. goto err_out;
  982. for (i = 0; i < PHY_MAX_ADDR; ++i)
  983. aup->mii_bus->irq[i] = PHY_POLL;
  984. /* if known, set corresponding PHY IRQs */
  985. if (aup->phy_static_config)
  986. if (aup->phy_irq && aup->phy_busid == aup->mac_id)
  987. aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
  988. err = mdiobus_register(aup->mii_bus);
  989. if (err) {
  990. dev_err(&pdev->dev, "failed to register MDIO bus\n");
  991. goto err_mdiobus_reg;
  992. }
  993. if (au1000_mii_probe(dev) != 0)
  994. goto err_out;
  995. pDBfree = NULL;
  996. /* setup the data buffer descriptors and attach a buffer to each one */
  997. pDB = aup->db;
  998. for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
  999. pDB->pnext = pDBfree;
  1000. pDBfree = pDB;
  1001. pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
  1002. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  1003. pDB++;
  1004. }
  1005. aup->pDBfree = pDBfree;
  1006. for (i = 0; i < NUM_RX_DMA; i++) {
  1007. pDB = au1000_GetFreeDB(aup);
  1008. if (!pDB)
  1009. goto err_out;
  1010. aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  1011. aup->rx_db_inuse[i] = pDB;
  1012. }
  1013. for (i = 0; i < NUM_TX_DMA; i++) {
  1014. pDB = au1000_GetFreeDB(aup);
  1015. if (!pDB)
  1016. goto err_out;
  1017. aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  1018. aup->tx_dma_ring[i]->len = 0;
  1019. aup->tx_db_inuse[i] = pDB;
  1020. }
  1021. dev->base_addr = base->start;
  1022. dev->irq = irq;
  1023. dev->netdev_ops = &au1000_netdev_ops;
  1024. SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
  1025. dev->watchdog_timeo = ETH_TX_TIMEOUT;
  1026. /*
  1027. * The boot code uses the ethernet controller, so reset it to start
  1028. * fresh. au1000_init() expects that the device is in reset state.
  1029. */
  1030. au1000_reset_mac(dev);
  1031. err = register_netdev(dev);
  1032. if (err) {
  1033. netdev_err(dev, "Cannot register net device, aborting.\n");
  1034. goto err_out;
  1035. }
  1036. netdev_info(dev, "Au1xx0 Ethernet found at 0x%lx, irq %d\n",
  1037. (unsigned long)base->start, irq);
  1038. if (version_printed++ == 0)
  1039. pr_info("%s version %s %s\n",
  1040. DRV_NAME, DRV_VERSION, DRV_AUTHOR);
  1041. return 0;
  1042. err_out:
  1043. if (aup->mii_bus != NULL)
  1044. mdiobus_unregister(aup->mii_bus);
  1045. /* here we should have a valid dev plus aup-> register addresses
  1046. * so we can reset the mac properly.
  1047. */
  1048. au1000_reset_mac(dev);
  1049. for (i = 0; i < NUM_RX_DMA; i++) {
  1050. if (aup->rx_db_inuse[i])
  1051. au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
  1052. }
  1053. for (i = 0; i < NUM_TX_DMA; i++) {
  1054. if (aup->tx_db_inuse[i])
  1055. au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
  1056. }
  1057. err_mdiobus_reg:
  1058. mdiobus_free(aup->mii_bus);
  1059. err_mdiobus_alloc:
  1060. iounmap(aup->macdma);
  1061. err_remap3:
  1062. iounmap(aup->enable);
  1063. err_remap2:
  1064. iounmap(aup->mac);
  1065. err_remap1:
  1066. dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
  1067. (void *)aup->vaddr, aup->dma_addr);
  1068. err_vaddr:
  1069. free_netdev(dev);
  1070. err_alloc:
  1071. release_mem_region(macdma->start, resource_size(macdma));
  1072. err_macdma:
  1073. release_mem_region(macen->start, resource_size(macen));
  1074. err_request:
  1075. release_mem_region(base->start, resource_size(base));
  1076. out:
  1077. return err;
  1078. }
  1079. static int __devexit au1000_remove(struct platform_device *pdev)
  1080. {
  1081. struct net_device *dev = platform_get_drvdata(pdev);
  1082. struct au1000_private *aup = netdev_priv(dev);
  1083. int i;
  1084. struct resource *base, *macen;
  1085. platform_set_drvdata(pdev, NULL);
  1086. unregister_netdev(dev);
  1087. mdiobus_unregister(aup->mii_bus);
  1088. mdiobus_free(aup->mii_bus);
  1089. for (i = 0; i < NUM_RX_DMA; i++)
  1090. if (aup->rx_db_inuse[i])
  1091. au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
  1092. for (i = 0; i < NUM_TX_DMA; i++)
  1093. if (aup->tx_db_inuse[i])
  1094. au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
  1095. dma_free_noncoherent(NULL, MAX_BUF_SIZE *
  1096. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  1097. (void *)aup->vaddr, aup->dma_addr);
  1098. iounmap(aup->macdma);
  1099. iounmap(aup->mac);
  1100. iounmap(aup->enable);
  1101. base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1102. release_mem_region(base->start, resource_size(base));
  1103. base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1104. release_mem_region(base->start, resource_size(base));
  1105. macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1106. release_mem_region(macen->start, resource_size(macen));
  1107. free_netdev(dev);
  1108. return 0;
  1109. }
  1110. static struct platform_driver au1000_eth_driver = {
  1111. .probe = au1000_probe,
  1112. .remove = __devexit_p(au1000_remove),
  1113. .driver = {
  1114. .name = "au1000-eth",
  1115. .owner = THIS_MODULE,
  1116. },
  1117. };
  1118. module_platform_driver(au1000_eth_driver);
  1119. MODULE_ALIAS("platform:au1000-eth");