au1000_eth.c 36 KB

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