au1000_eth.c 35 KB

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