au1000_eth.c 37 KB

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