au1000_eth.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
  316. * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII 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 au1000_mdio_read(dev, phy_addr, regnum);
  324. }
  325. static int au1000_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  326. u16 value)
  327. {
  328. struct net_device *const dev = bus->priv;
  329. /* make sure the MAC associated with this
  330. * mii_bus is enabled
  331. */
  332. au1000_enable_mac(dev, 0);
  333. au1000_mdio_write(dev, phy_addr, regnum, value);
  334. return 0;
  335. }
  336. static int au1000_mdiobus_reset(struct mii_bus *bus)
  337. {
  338. struct net_device *const dev = bus->priv;
  339. /* make sure the MAC associated with this
  340. * mii_bus is enabled
  341. */
  342. au1000_enable_mac(dev, 0);
  343. return 0;
  344. }
  345. static void au1000_hard_stop(struct net_device *dev)
  346. {
  347. struct au1000_private *aup = netdev_priv(dev);
  348. u32 reg;
  349. netif_dbg(aup, drv, dev, "hard stop\n");
  350. reg = readl(&aup->mac->control);
  351. reg &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
  352. writel(reg, &aup->mac->control);
  353. wmb(); /* drain writebuffer */
  354. mdelay(10);
  355. }
  356. static void au1000_enable_rx_tx(struct net_device *dev)
  357. {
  358. struct au1000_private *aup = netdev_priv(dev);
  359. u32 reg;
  360. netif_dbg(aup, hw, dev, "enable_rx_tx\n");
  361. reg = readl(&aup->mac->control);
  362. reg |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
  363. writel(reg, &aup->mac->control);
  364. wmb(); /* drain writebuffer */
  365. mdelay(10);
  366. }
  367. static void
  368. au1000_adjust_link(struct net_device *dev)
  369. {
  370. struct au1000_private *aup = netdev_priv(dev);
  371. struct phy_device *phydev = aup->phy_dev;
  372. unsigned long flags;
  373. u32 reg;
  374. int status_change = 0;
  375. BUG_ON(!aup->phy_dev);
  376. spin_lock_irqsave(&aup->lock, flags);
  377. if (phydev->link && (aup->old_speed != phydev->speed)) {
  378. /* speed changed */
  379. switch (phydev->speed) {
  380. case SPEED_10:
  381. case SPEED_100:
  382. break;
  383. default:
  384. netdev_warn(dev, "Speed (%d) is not 10/100 ???\n",
  385. phydev->speed);
  386. break;
  387. }
  388. aup->old_speed = phydev->speed;
  389. status_change = 1;
  390. }
  391. if (phydev->link && (aup->old_duplex != phydev->duplex)) {
  392. /* duplex mode changed */
  393. /* switching duplex mode requires to disable rx and tx! */
  394. au1000_hard_stop(dev);
  395. reg = readl(&aup->mac->control);
  396. if (DUPLEX_FULL == phydev->duplex) {
  397. reg |= MAC_FULL_DUPLEX;
  398. reg &= ~MAC_DISABLE_RX_OWN;
  399. } else {
  400. reg &= ~MAC_FULL_DUPLEX;
  401. reg |= MAC_DISABLE_RX_OWN;
  402. }
  403. writel(reg, &aup->mac->control);
  404. wmb(); /* drain writebuffer */
  405. mdelay(1);
  406. au1000_enable_rx_tx(dev);
  407. aup->old_duplex = phydev->duplex;
  408. status_change = 1;
  409. }
  410. if (phydev->link != aup->old_link) {
  411. /* link state changed */
  412. if (!phydev->link) {
  413. /* link went down */
  414. aup->old_speed = 0;
  415. aup->old_duplex = -1;
  416. }
  417. aup->old_link = phydev->link;
  418. status_change = 1;
  419. }
  420. spin_unlock_irqrestore(&aup->lock, flags);
  421. if (status_change) {
  422. if (phydev->link)
  423. netdev_info(dev, "link up (%d/%s)\n",
  424. phydev->speed,
  425. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  426. else
  427. netdev_info(dev, "link down\n");
  428. }
  429. }
  430. static int au1000_mii_probe(struct net_device *dev)
  431. {
  432. struct au1000_private *const aup = netdev_priv(dev);
  433. struct phy_device *phydev = NULL;
  434. int phy_addr;
  435. if (aup->phy_static_config) {
  436. BUG_ON(aup->mac_id < 0 || aup->mac_id > 1);
  437. if (aup->phy_addr)
  438. phydev = aup->mii_bus->phy_map[aup->phy_addr];
  439. else
  440. netdev_info(dev, "using PHY-less setup\n");
  441. return 0;
  442. }
  443. /* find the first (lowest address) PHY
  444. * on the current MAC's MII bus
  445. */
  446. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
  447. if (aup->mii_bus->phy_map[phy_addr]) {
  448. phydev = aup->mii_bus->phy_map[phy_addr];
  449. if (!aup->phy_search_highest_addr)
  450. /* break out with first one found */
  451. break;
  452. }
  453. if (aup->phy1_search_mac0) {
  454. /* try harder to find a PHY */
  455. if (!phydev && (aup->mac_id == 1)) {
  456. /* no PHY found, maybe we have a dual PHY? */
  457. dev_info(&dev->dev, ": no PHY found on MAC1, "
  458. "let's see if it's attached to MAC0...\n");
  459. /* find the first (lowest address) non-attached
  460. * PHY on the MAC0 MII bus
  461. */
  462. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  463. struct phy_device *const tmp_phydev =
  464. aup->mii_bus->phy_map[phy_addr];
  465. if (aup->mac_id == 1)
  466. break;
  467. /* no PHY here... */
  468. if (!tmp_phydev)
  469. continue;
  470. /* already claimed by MAC0 */
  471. if (tmp_phydev->attached_dev)
  472. continue;
  473. phydev = tmp_phydev;
  474. break; /* found it */
  475. }
  476. }
  477. }
  478. if (!phydev) {
  479. netdev_err(dev, "no PHY found\n");
  480. return -1;
  481. }
  482. /* now we are supposed to have a proper phydev, to attach to... */
  483. BUG_ON(phydev->attached_dev);
  484. phydev = phy_connect(dev, dev_name(&phydev->dev),
  485. &au1000_adjust_link, PHY_INTERFACE_MODE_MII);
  486. if (IS_ERR(phydev)) {
  487. netdev_err(dev, "Could not attach to PHY\n");
  488. return PTR_ERR(phydev);
  489. }
  490. /* mask with MAC supported features */
  491. phydev->supported &= (SUPPORTED_10baseT_Half
  492. | SUPPORTED_10baseT_Full
  493. | SUPPORTED_100baseT_Half
  494. | SUPPORTED_100baseT_Full
  495. | SUPPORTED_Autoneg
  496. /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
  497. | SUPPORTED_MII
  498. | SUPPORTED_TP);
  499. phydev->advertising = phydev->supported;
  500. aup->old_link = 0;
  501. aup->old_speed = 0;
  502. aup->old_duplex = -1;
  503. aup->phy_dev = phydev;
  504. netdev_info(dev, "attached PHY driver [%s] "
  505. "(mii_bus:phy_addr=%s, irq=%d)\n",
  506. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  507. return 0;
  508. }
  509. /*
  510. * Buffer allocation/deallocation routines. The buffer descriptor returned
  511. * has the virtual and dma address of a buffer suitable for
  512. * both, receive and transmit operations.
  513. */
  514. static struct db_dest *au1000_GetFreeDB(struct au1000_private *aup)
  515. {
  516. struct db_dest *pDB;
  517. pDB = aup->pDBfree;
  518. if (pDB)
  519. aup->pDBfree = pDB->pnext;
  520. return pDB;
  521. }
  522. void au1000_ReleaseDB(struct au1000_private *aup, struct db_dest *pDB)
  523. {
  524. struct db_dest *pDBfree = aup->pDBfree;
  525. if (pDBfree)
  526. pDBfree->pnext = pDB;
  527. aup->pDBfree = pDB;
  528. }
  529. static void au1000_reset_mac_unlocked(struct net_device *dev)
  530. {
  531. struct au1000_private *const aup = netdev_priv(dev);
  532. int i;
  533. au1000_hard_stop(dev);
  534. writel(MAC_EN_CLOCK_ENABLE, aup->enable);
  535. wmb(); /* drain writebuffer */
  536. mdelay(2);
  537. writel(0, aup->enable);
  538. wmb(); /* drain writebuffer */
  539. mdelay(2);
  540. aup->tx_full = 0;
  541. for (i = 0; i < NUM_RX_DMA; i++) {
  542. /* reset control bits */
  543. aup->rx_dma_ring[i]->buff_stat &= ~0xf;
  544. }
  545. for (i = 0; i < NUM_TX_DMA; i++) {
  546. /* reset control bits */
  547. aup->tx_dma_ring[i]->buff_stat &= ~0xf;
  548. }
  549. aup->mac_enabled = 0;
  550. }
  551. static void au1000_reset_mac(struct net_device *dev)
  552. {
  553. struct au1000_private *const aup = netdev_priv(dev);
  554. unsigned long flags;
  555. netif_dbg(aup, hw, dev, "reset mac, aup %x\n",
  556. (unsigned)aup);
  557. spin_lock_irqsave(&aup->lock, flags);
  558. au1000_reset_mac_unlocked(dev);
  559. spin_unlock_irqrestore(&aup->lock, flags);
  560. }
  561. /*
  562. * Setup the receive and transmit "rings". These pointers are the addresses
  563. * of the rx and tx MAC DMA registers so they are fixed by the hardware --
  564. * these are not descriptors sitting in memory.
  565. */
  566. static void
  567. au1000_setup_hw_rings(struct au1000_private *aup, void __iomem *tx_base)
  568. {
  569. int i;
  570. for (i = 0; i < NUM_RX_DMA; i++) {
  571. aup->rx_dma_ring[i] = (struct rx_dma *)
  572. (tx_base + 0x100 + sizeof(struct rx_dma) * i);
  573. }
  574. for (i = 0; i < NUM_TX_DMA; i++) {
  575. aup->tx_dma_ring[i] = (struct tx_dma *)
  576. (tx_base + sizeof(struct tx_dma) * i);
  577. }
  578. }
  579. /*
  580. * ethtool operations
  581. */
  582. static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  583. {
  584. struct au1000_private *aup = netdev_priv(dev);
  585. if (aup->phy_dev)
  586. return phy_ethtool_gset(aup->phy_dev, cmd);
  587. return -EINVAL;
  588. }
  589. static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  590. {
  591. struct au1000_private *aup = netdev_priv(dev);
  592. if (!capable(CAP_NET_ADMIN))
  593. return -EPERM;
  594. if (aup->phy_dev)
  595. return phy_ethtool_sset(aup->phy_dev, cmd);
  596. return -EINVAL;
  597. }
  598. static void
  599. au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  600. {
  601. struct au1000_private *aup = netdev_priv(dev);
  602. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  603. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  604. snprintf(info->bus_info, sizeof(info->bus_info), "%s %d", DRV_NAME,
  605. aup->mac_id);
  606. }
  607. static void au1000_set_msglevel(struct net_device *dev, u32 value)
  608. {
  609. struct au1000_private *aup = netdev_priv(dev);
  610. aup->msg_enable = value;
  611. }
  612. static u32 au1000_get_msglevel(struct net_device *dev)
  613. {
  614. struct au1000_private *aup = netdev_priv(dev);
  615. return aup->msg_enable;
  616. }
  617. static const struct ethtool_ops au1000_ethtool_ops = {
  618. .get_settings = au1000_get_settings,
  619. .set_settings = au1000_set_settings,
  620. .get_drvinfo = au1000_get_drvinfo,
  621. .get_link = ethtool_op_get_link,
  622. .get_msglevel = au1000_get_msglevel,
  623. .set_msglevel = au1000_set_msglevel,
  624. };
  625. /*
  626. * Initialize the interface.
  627. *
  628. * When the device powers up, the clocks are disabled and the
  629. * mac is in reset state. When the interface is closed, we
  630. * do the same -- reset the device and disable the clocks to
  631. * conserve power. Thus, whenever au1000_init() is called,
  632. * the device should already be in reset state.
  633. */
  634. static int au1000_init(struct net_device *dev)
  635. {
  636. struct au1000_private *aup = netdev_priv(dev);
  637. unsigned long flags;
  638. int i;
  639. u32 control;
  640. netif_dbg(aup, hw, dev, "au1000_init\n");
  641. /* bring the device out of reset */
  642. au1000_enable_mac(dev, 1);
  643. spin_lock_irqsave(&aup->lock, flags);
  644. writel(0, &aup->mac->control);
  645. aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
  646. aup->tx_tail = aup->tx_head;
  647. aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
  648. writel(dev->dev_addr[5]<<8 | dev->dev_addr[4],
  649. &aup->mac->mac_addr_high);
  650. writel(dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
  651. dev->dev_addr[1]<<8 | dev->dev_addr[0],
  652. &aup->mac->mac_addr_low);
  653. for (i = 0; i < NUM_RX_DMA; i++)
  654. aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
  655. wmb(); /* drain writebuffer */
  656. control = MAC_RX_ENABLE | MAC_TX_ENABLE;
  657. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  658. control |= MAC_BIG_ENDIAN;
  659. #endif
  660. if (aup->phy_dev) {
  661. if (aup->phy_dev->link && (DUPLEX_FULL == aup->phy_dev->duplex))
  662. control |= MAC_FULL_DUPLEX;
  663. else
  664. control |= MAC_DISABLE_RX_OWN;
  665. } else { /* PHY-less op, assume full-duplex */
  666. control |= MAC_FULL_DUPLEX;
  667. }
  668. writel(control, &aup->mac->control);
  669. writel(0x8100, &aup->mac->vlan1_tag); /* activate vlan support */
  670. wmb(); /* drain writebuffer */
  671. spin_unlock_irqrestore(&aup->lock, flags);
  672. return 0;
  673. }
  674. static inline void au1000_update_rx_stats(struct net_device *dev, u32 status)
  675. {
  676. struct net_device_stats *ps = &dev->stats;
  677. ps->rx_packets++;
  678. if (status & RX_MCAST_FRAME)
  679. ps->multicast++;
  680. if (status & RX_ERROR) {
  681. ps->rx_errors++;
  682. if (status & RX_MISSED_FRAME)
  683. ps->rx_missed_errors++;
  684. if (status & (RX_OVERLEN | RX_RUNT | RX_LEN_ERROR))
  685. ps->rx_length_errors++;
  686. if (status & RX_CRC_ERROR)
  687. ps->rx_crc_errors++;
  688. if (status & RX_COLL)
  689. ps->collisions++;
  690. } else
  691. ps->rx_bytes += status & RX_FRAME_LEN_MASK;
  692. }
  693. /*
  694. * Au1000 receive routine.
  695. */
  696. static int au1000_rx(struct net_device *dev)
  697. {
  698. struct au1000_private *aup = netdev_priv(dev);
  699. struct sk_buff *skb;
  700. struct rx_dma *prxd;
  701. u32 buff_stat, status;
  702. struct db_dest *pDB;
  703. u32 frmlen;
  704. netif_dbg(aup, rx_status, dev, "au1000_rx head %d\n", aup->rx_head);
  705. prxd = aup->rx_dma_ring[aup->rx_head];
  706. buff_stat = prxd->buff_stat;
  707. while (buff_stat & RX_T_DONE) {
  708. status = prxd->status;
  709. pDB = aup->rx_db_inuse[aup->rx_head];
  710. au1000_update_rx_stats(dev, status);
  711. if (!(status & RX_ERROR)) {
  712. /* good frame */
  713. frmlen = (status & RX_FRAME_LEN_MASK);
  714. frmlen -= 4; /* Remove FCS */
  715. skb = netdev_alloc_skb(dev, frmlen + 2);
  716. if (skb == NULL) {
  717. dev->stats.rx_dropped++;
  718. continue;
  719. }
  720. skb_reserve(skb, 2); /* 16 byte IP header align */
  721. skb_copy_to_linear_data(skb,
  722. (unsigned char *)pDB->vaddr, frmlen);
  723. skb_put(skb, frmlen);
  724. skb->protocol = eth_type_trans(skb, dev);
  725. netif_rx(skb); /* pass the packet to upper layers */
  726. } else {
  727. if (au1000_debug > 4) {
  728. pr_err("rx_error(s):");
  729. if (status & RX_MISSED_FRAME)
  730. pr_cont(" miss");
  731. if (status & RX_WDOG_TIMER)
  732. pr_cont(" wdog");
  733. if (status & RX_RUNT)
  734. pr_cont(" runt");
  735. if (status & RX_OVERLEN)
  736. pr_cont(" overlen");
  737. if (status & RX_COLL)
  738. pr_cont(" coll");
  739. if (status & RX_MII_ERROR)
  740. pr_cont(" mii error");
  741. if (status & RX_CRC_ERROR)
  742. pr_cont(" crc error");
  743. if (status & RX_LEN_ERROR)
  744. pr_cont(" len error");
  745. if (status & RX_U_CNTRL_FRAME)
  746. pr_cont(" u control frame");
  747. pr_cont("\n");
  748. }
  749. }
  750. prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
  751. aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
  752. wmb(); /* drain writebuffer */
  753. /* next descriptor */
  754. prxd = aup->rx_dma_ring[aup->rx_head];
  755. buff_stat = prxd->buff_stat;
  756. }
  757. return 0;
  758. }
  759. static void au1000_update_tx_stats(struct net_device *dev, u32 status)
  760. {
  761. struct au1000_private *aup = netdev_priv(dev);
  762. struct net_device_stats *ps = &dev->stats;
  763. if (status & TX_FRAME_ABORTED) {
  764. if (!aup->phy_dev || (DUPLEX_FULL == aup->phy_dev->duplex)) {
  765. if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
  766. /* any other tx errors are only valid
  767. * in half duplex mode
  768. */
  769. ps->tx_errors++;
  770. ps->tx_aborted_errors++;
  771. }
  772. } else {
  773. ps->tx_errors++;
  774. ps->tx_aborted_errors++;
  775. if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
  776. ps->tx_carrier_errors++;
  777. }
  778. }
  779. }
  780. /*
  781. * Called from the interrupt service routine to acknowledge
  782. * the TX DONE bits. This is a must if the irq is setup as
  783. * edge triggered.
  784. */
  785. static void au1000_tx_ack(struct net_device *dev)
  786. {
  787. struct au1000_private *aup = netdev_priv(dev);
  788. struct tx_dma *ptxd;
  789. ptxd = aup->tx_dma_ring[aup->tx_tail];
  790. while (ptxd->buff_stat & TX_T_DONE) {
  791. au1000_update_tx_stats(dev, ptxd->status);
  792. ptxd->buff_stat &= ~TX_T_DONE;
  793. ptxd->len = 0;
  794. wmb(); /* drain writebuffer */
  795. aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
  796. ptxd = aup->tx_dma_ring[aup->tx_tail];
  797. if (aup->tx_full) {
  798. aup->tx_full = 0;
  799. netif_wake_queue(dev);
  800. }
  801. }
  802. }
  803. /*
  804. * Au1000 interrupt service routine.
  805. */
  806. static irqreturn_t au1000_interrupt(int irq, void *dev_id)
  807. {
  808. struct net_device *dev = dev_id;
  809. /* Handle RX interrupts first to minimize chance of overrun */
  810. au1000_rx(dev);
  811. au1000_tx_ack(dev);
  812. return IRQ_RETVAL(1);
  813. }
  814. static int au1000_open(struct net_device *dev)
  815. {
  816. int retval;
  817. struct au1000_private *aup = netdev_priv(dev);
  818. netif_dbg(aup, drv, dev, "open: dev=%p\n", dev);
  819. retval = request_irq(dev->irq, au1000_interrupt, 0,
  820. dev->name, dev);
  821. if (retval) {
  822. netdev_err(dev, "unable to get IRQ %d\n", dev->irq);
  823. return retval;
  824. }
  825. retval = au1000_init(dev);
  826. if (retval) {
  827. netdev_err(dev, "error in au1000_init\n");
  828. free_irq(dev->irq, dev);
  829. return retval;
  830. }
  831. if (aup->phy_dev) {
  832. /* cause the PHY state machine to schedule a link state check */
  833. aup->phy_dev->state = PHY_CHANGELINK;
  834. phy_start(aup->phy_dev);
  835. }
  836. netif_start_queue(dev);
  837. netif_dbg(aup, drv, dev, "open: Initialization done.\n");
  838. return 0;
  839. }
  840. static int au1000_close(struct net_device *dev)
  841. {
  842. unsigned long flags;
  843. struct au1000_private *const aup = netdev_priv(dev);
  844. netif_dbg(aup, drv, dev, "close: dev=%p\n", dev);
  845. if (aup->phy_dev)
  846. phy_stop(aup->phy_dev);
  847. spin_lock_irqsave(&aup->lock, flags);
  848. au1000_reset_mac_unlocked(dev);
  849. /* stop the device */
  850. netif_stop_queue(dev);
  851. /* disable the interrupt */
  852. free_irq(dev->irq, dev);
  853. spin_unlock_irqrestore(&aup->lock, flags);
  854. return 0;
  855. }
  856. /*
  857. * Au1000 transmit routine.
  858. */
  859. static netdev_tx_t au1000_tx(struct sk_buff *skb, struct net_device *dev)
  860. {
  861. struct au1000_private *aup = netdev_priv(dev);
  862. struct net_device_stats *ps = &dev->stats;
  863. struct tx_dma *ptxd;
  864. u32 buff_stat;
  865. struct db_dest *pDB;
  866. int i;
  867. netif_dbg(aup, tx_queued, dev, "tx: aup %x len=%d, data=%p, head %d\n",
  868. (unsigned)aup, skb->len,
  869. skb->data, aup->tx_head);
  870. ptxd = aup->tx_dma_ring[aup->tx_head];
  871. buff_stat = ptxd->buff_stat;
  872. if (buff_stat & TX_DMA_ENABLE) {
  873. /* We've wrapped around and the transmitter is still busy */
  874. netif_stop_queue(dev);
  875. aup->tx_full = 1;
  876. return NETDEV_TX_BUSY;
  877. } else if (buff_stat & TX_T_DONE) {
  878. au1000_update_tx_stats(dev, ptxd->status);
  879. ptxd->len = 0;
  880. }
  881. if (aup->tx_full) {
  882. aup->tx_full = 0;
  883. netif_wake_queue(dev);
  884. }
  885. pDB = aup->tx_db_inuse[aup->tx_head];
  886. skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
  887. if (skb->len < ETH_ZLEN) {
  888. for (i = skb->len; i < ETH_ZLEN; i++)
  889. ((char *)pDB->vaddr)[i] = 0;
  890. ptxd->len = ETH_ZLEN;
  891. } else
  892. ptxd->len = skb->len;
  893. ps->tx_packets++;
  894. ps->tx_bytes += ptxd->len;
  895. ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
  896. wmb(); /* drain writebuffer */
  897. dev_kfree_skb(skb);
  898. aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
  899. return NETDEV_TX_OK;
  900. }
  901. /*
  902. * The Tx ring has been full longer than the watchdog timeout
  903. * value. The transmitter must be hung?
  904. */
  905. static void au1000_tx_timeout(struct net_device *dev)
  906. {
  907. netdev_err(dev, "au1000_tx_timeout: dev=%p\n", dev);
  908. au1000_reset_mac(dev);
  909. au1000_init(dev);
  910. dev->trans_start = jiffies; /* prevent tx timeout */
  911. netif_wake_queue(dev);
  912. }
  913. static void au1000_multicast_list(struct net_device *dev)
  914. {
  915. struct au1000_private *aup = netdev_priv(dev);
  916. u32 reg;
  917. netif_dbg(aup, drv, dev, "%s: flags=%x\n", __func__, dev->flags);
  918. reg = readl(&aup->mac->control);
  919. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  920. reg |= MAC_PROMISCUOUS;
  921. } else if ((dev->flags & IFF_ALLMULTI) ||
  922. netdev_mc_count(dev) > MULTICAST_FILTER_LIMIT) {
  923. reg |= MAC_PASS_ALL_MULTI;
  924. reg &= ~MAC_PROMISCUOUS;
  925. netdev_info(dev, "Pass all multicast\n");
  926. } else {
  927. struct netdev_hw_addr *ha;
  928. u32 mc_filter[2]; /* Multicast hash filter */
  929. mc_filter[1] = mc_filter[0] = 0;
  930. netdev_for_each_mc_addr(ha, dev)
  931. set_bit(ether_crc(ETH_ALEN, ha->addr)>>26,
  932. (long *)mc_filter);
  933. writel(mc_filter[1], &aup->mac->multi_hash_high);
  934. writel(mc_filter[0], &aup->mac->multi_hash_low);
  935. reg &= ~MAC_PROMISCUOUS;
  936. reg |= MAC_HASH_MODE;
  937. }
  938. writel(reg, &aup->mac->control);
  939. }
  940. static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  941. {
  942. struct au1000_private *aup = netdev_priv(dev);
  943. if (!netif_running(dev))
  944. return -EINVAL;
  945. if (!aup->phy_dev)
  946. return -EINVAL; /* PHY not controllable */
  947. return phy_mii_ioctl(aup->phy_dev, rq, cmd);
  948. }
  949. static const struct net_device_ops au1000_netdev_ops = {
  950. .ndo_open = au1000_open,
  951. .ndo_stop = au1000_close,
  952. .ndo_start_xmit = au1000_tx,
  953. .ndo_set_rx_mode = au1000_multicast_list,
  954. .ndo_do_ioctl = au1000_ioctl,
  955. .ndo_tx_timeout = au1000_tx_timeout,
  956. .ndo_set_mac_address = eth_mac_addr,
  957. .ndo_validate_addr = eth_validate_addr,
  958. .ndo_change_mtu = eth_change_mtu,
  959. };
  960. static int au1000_probe(struct platform_device *pdev)
  961. {
  962. struct au1000_private *aup = NULL;
  963. struct au1000_eth_platform_data *pd;
  964. struct net_device *dev = NULL;
  965. struct db_dest *pDB, *pDBfree;
  966. int irq, i, err = 0;
  967. struct resource *base, *macen, *macdma;
  968. base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  969. if (!base) {
  970. dev_err(&pdev->dev, "failed to retrieve base register\n");
  971. err = -ENODEV;
  972. goto out;
  973. }
  974. macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  975. if (!macen) {
  976. dev_err(&pdev->dev, "failed to retrieve MAC Enable register\n");
  977. err = -ENODEV;
  978. goto out;
  979. }
  980. irq = platform_get_irq(pdev, 0);
  981. if (irq < 0) {
  982. dev_err(&pdev->dev, "failed to retrieve IRQ\n");
  983. err = -ENODEV;
  984. goto out;
  985. }
  986. macdma = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  987. if (!macdma) {
  988. dev_err(&pdev->dev, "failed to retrieve MACDMA registers\n");
  989. err = -ENODEV;
  990. goto out;
  991. }
  992. if (!request_mem_region(base->start, resource_size(base),
  993. pdev->name)) {
  994. dev_err(&pdev->dev, "failed to request memory region for base registers\n");
  995. err = -ENXIO;
  996. goto out;
  997. }
  998. if (!request_mem_region(macen->start, resource_size(macen),
  999. pdev->name)) {
  1000. dev_err(&pdev->dev, "failed to request memory region for MAC enable register\n");
  1001. err = -ENXIO;
  1002. goto err_request;
  1003. }
  1004. if (!request_mem_region(macdma->start, resource_size(macdma),
  1005. pdev->name)) {
  1006. dev_err(&pdev->dev, "failed to request MACDMA memory region\n");
  1007. err = -ENXIO;
  1008. goto err_macdma;
  1009. }
  1010. dev = alloc_etherdev(sizeof(struct au1000_private));
  1011. if (!dev) {
  1012. err = -ENOMEM;
  1013. goto err_alloc;
  1014. }
  1015. SET_NETDEV_DEV(dev, &pdev->dev);
  1016. platform_set_drvdata(pdev, dev);
  1017. aup = netdev_priv(dev);
  1018. spin_lock_init(&aup->lock);
  1019. aup->msg_enable = (au1000_debug < 4 ?
  1020. AU1000_DEF_MSG_ENABLE : au1000_debug);
  1021. /* Allocate the data buffers
  1022. * Snooping works fine with eth on all au1xxx
  1023. */
  1024. aup->vaddr = (u32)dma_alloc_noncoherent(NULL, MAX_BUF_SIZE *
  1025. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  1026. &aup->dma_addr, 0);
  1027. if (!aup->vaddr) {
  1028. dev_err(&pdev->dev, "failed to allocate data buffers\n");
  1029. err = -ENOMEM;
  1030. goto err_vaddr;
  1031. }
  1032. /* aup->mac is the base address of the MAC's registers */
  1033. aup->mac = (struct mac_reg *)
  1034. ioremap_nocache(base->start, resource_size(base));
  1035. if (!aup->mac) {
  1036. dev_err(&pdev->dev, "failed to ioremap MAC registers\n");
  1037. err = -ENXIO;
  1038. goto err_remap1;
  1039. }
  1040. /* Setup some variables for quick register address access */
  1041. aup->enable = (u32 *)ioremap_nocache(macen->start,
  1042. resource_size(macen));
  1043. if (!aup->enable) {
  1044. dev_err(&pdev->dev, "failed to ioremap MAC enable register\n");
  1045. err = -ENXIO;
  1046. goto err_remap2;
  1047. }
  1048. aup->mac_id = pdev->id;
  1049. aup->macdma = ioremap_nocache(macdma->start, resource_size(macdma));
  1050. if (!aup->macdma) {
  1051. dev_err(&pdev->dev, "failed to ioremap MACDMA registers\n");
  1052. err = -ENXIO;
  1053. goto err_remap3;
  1054. }
  1055. au1000_setup_hw_rings(aup, aup->macdma);
  1056. writel(0, aup->enable);
  1057. aup->mac_enabled = 0;
  1058. pd = dev_get_platdata(&pdev->dev);
  1059. if (!pd) {
  1060. dev_info(&pdev->dev, "no platform_data passed,"
  1061. " PHY search on MAC0\n");
  1062. aup->phy1_search_mac0 = 1;
  1063. } else {
  1064. if (is_valid_ether_addr(pd->mac)) {
  1065. memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
  1066. } else {
  1067. /* Set a random MAC since no valid provided by platform_data. */
  1068. eth_hw_addr_random(dev);
  1069. }
  1070. aup->phy_static_config = pd->phy_static_config;
  1071. aup->phy_search_highest_addr = pd->phy_search_highest_addr;
  1072. aup->phy1_search_mac0 = pd->phy1_search_mac0;
  1073. aup->phy_addr = pd->phy_addr;
  1074. aup->phy_busid = pd->phy_busid;
  1075. aup->phy_irq = pd->phy_irq;
  1076. }
  1077. if (aup->phy_busid && aup->phy_busid > 0) {
  1078. dev_err(&pdev->dev, "MAC0-associated PHY attached 2nd MACs MII bus not supported yet\n");
  1079. err = -ENODEV;
  1080. goto err_mdiobus_alloc;
  1081. }
  1082. aup->mii_bus = mdiobus_alloc();
  1083. if (aup->mii_bus == NULL) {
  1084. dev_err(&pdev->dev, "failed to allocate mdiobus structure\n");
  1085. err = -ENOMEM;
  1086. goto err_mdiobus_alloc;
  1087. }
  1088. aup->mii_bus->priv = dev;
  1089. aup->mii_bus->read = au1000_mdiobus_read;
  1090. aup->mii_bus->write = au1000_mdiobus_write;
  1091. aup->mii_bus->reset = au1000_mdiobus_reset;
  1092. aup->mii_bus->name = "au1000_eth_mii";
  1093. snprintf(aup->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1094. pdev->name, aup->mac_id);
  1095. aup->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  1096. if (aup->mii_bus->irq == NULL) {
  1097. err = -ENOMEM;
  1098. goto err_out;
  1099. }
  1100. for (i = 0; i < PHY_MAX_ADDR; ++i)
  1101. aup->mii_bus->irq[i] = PHY_POLL;
  1102. /* if known, set corresponding PHY IRQs */
  1103. if (aup->phy_static_config)
  1104. if (aup->phy_irq && aup->phy_busid == aup->mac_id)
  1105. aup->mii_bus->irq[aup->phy_addr] = aup->phy_irq;
  1106. err = mdiobus_register(aup->mii_bus);
  1107. if (err) {
  1108. dev_err(&pdev->dev, "failed to register MDIO bus\n");
  1109. goto err_mdiobus_reg;
  1110. }
  1111. err = au1000_mii_probe(dev);
  1112. if (err != 0)
  1113. goto err_out;
  1114. pDBfree = NULL;
  1115. /* setup the data buffer descriptors and attach a buffer to each one */
  1116. pDB = aup->db;
  1117. for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
  1118. pDB->pnext = pDBfree;
  1119. pDBfree = pDB;
  1120. pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
  1121. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  1122. pDB++;
  1123. }
  1124. aup->pDBfree = pDBfree;
  1125. err = -ENODEV;
  1126. for (i = 0; i < NUM_RX_DMA; i++) {
  1127. pDB = au1000_GetFreeDB(aup);
  1128. if (!pDB)
  1129. goto err_out;
  1130. aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  1131. aup->rx_db_inuse[i] = pDB;
  1132. }
  1133. err = -ENODEV;
  1134. for (i = 0; i < NUM_TX_DMA; i++) {
  1135. pDB = au1000_GetFreeDB(aup);
  1136. if (!pDB)
  1137. goto err_out;
  1138. aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
  1139. aup->tx_dma_ring[i]->len = 0;
  1140. aup->tx_db_inuse[i] = pDB;
  1141. }
  1142. dev->base_addr = base->start;
  1143. dev->irq = irq;
  1144. dev->netdev_ops = &au1000_netdev_ops;
  1145. dev->ethtool_ops = &au1000_ethtool_ops;
  1146. dev->watchdog_timeo = ETH_TX_TIMEOUT;
  1147. /*
  1148. * The boot code uses the ethernet controller, so reset it to start
  1149. * fresh. au1000_init() expects that the device is in reset state.
  1150. */
  1151. au1000_reset_mac(dev);
  1152. err = register_netdev(dev);
  1153. if (err) {
  1154. netdev_err(dev, "Cannot register net device, aborting.\n");
  1155. goto err_out;
  1156. }
  1157. netdev_info(dev, "Au1xx0 Ethernet found at 0x%lx, irq %d\n",
  1158. (unsigned long)base->start, irq);
  1159. pr_info_once("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
  1160. return 0;
  1161. err_out:
  1162. if (aup->mii_bus != NULL)
  1163. mdiobus_unregister(aup->mii_bus);
  1164. /* here we should have a valid dev plus aup-> register addresses
  1165. * so we can reset the mac properly.
  1166. */
  1167. au1000_reset_mac(dev);
  1168. for (i = 0; i < NUM_RX_DMA; i++) {
  1169. if (aup->rx_db_inuse[i])
  1170. au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
  1171. }
  1172. for (i = 0; i < NUM_TX_DMA; i++) {
  1173. if (aup->tx_db_inuse[i])
  1174. au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
  1175. }
  1176. err_mdiobus_reg:
  1177. mdiobus_free(aup->mii_bus);
  1178. err_mdiobus_alloc:
  1179. iounmap(aup->macdma);
  1180. err_remap3:
  1181. iounmap(aup->enable);
  1182. err_remap2:
  1183. iounmap(aup->mac);
  1184. err_remap1:
  1185. dma_free_noncoherent(NULL, MAX_BUF_SIZE * (NUM_TX_BUFFS + NUM_RX_BUFFS),
  1186. (void *)aup->vaddr, aup->dma_addr);
  1187. err_vaddr:
  1188. free_netdev(dev);
  1189. err_alloc:
  1190. release_mem_region(macdma->start, resource_size(macdma));
  1191. err_macdma:
  1192. release_mem_region(macen->start, resource_size(macen));
  1193. err_request:
  1194. release_mem_region(base->start, resource_size(base));
  1195. out:
  1196. return err;
  1197. }
  1198. static int au1000_remove(struct platform_device *pdev)
  1199. {
  1200. struct net_device *dev = platform_get_drvdata(pdev);
  1201. struct au1000_private *aup = netdev_priv(dev);
  1202. int i;
  1203. struct resource *base, *macen;
  1204. unregister_netdev(dev);
  1205. mdiobus_unregister(aup->mii_bus);
  1206. mdiobus_free(aup->mii_bus);
  1207. for (i = 0; i < NUM_RX_DMA; i++)
  1208. if (aup->rx_db_inuse[i])
  1209. au1000_ReleaseDB(aup, aup->rx_db_inuse[i]);
  1210. for (i = 0; i < NUM_TX_DMA; i++)
  1211. if (aup->tx_db_inuse[i])
  1212. au1000_ReleaseDB(aup, aup->tx_db_inuse[i]);
  1213. dma_free_noncoherent(NULL, MAX_BUF_SIZE *
  1214. (NUM_TX_BUFFS + NUM_RX_BUFFS),
  1215. (void *)aup->vaddr, aup->dma_addr);
  1216. iounmap(aup->macdma);
  1217. iounmap(aup->mac);
  1218. iounmap(aup->enable);
  1219. base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1220. release_mem_region(base->start, resource_size(base));
  1221. base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1222. release_mem_region(base->start, resource_size(base));
  1223. macen = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1224. release_mem_region(macen->start, resource_size(macen));
  1225. free_netdev(dev);
  1226. return 0;
  1227. }
  1228. static struct platform_driver au1000_eth_driver = {
  1229. .probe = au1000_probe,
  1230. .remove = au1000_remove,
  1231. .driver = {
  1232. .name = "au1000-eth",
  1233. },
  1234. };
  1235. module_platform_driver(au1000_eth_driver);
  1236. MODULE_ALIAS("platform:au1000-eth");