au1000_eth.c 33 KB

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