au1000_eth.c 34 KB

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