mv643xx_eth.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  1. /*
  2. * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
  3. * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
  4. *
  5. * Based on the 64360 driver from:
  6. * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
  7. * Rabeeh Khoury <rabeeh@marvell.com>
  8. *
  9. * Copyright (C) 2003 PMC-Sierra, Inc.,
  10. * written by Manish Lachwani
  11. *
  12. * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
  13. *
  14. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  15. * Dale Farnsworth <dale@farnsworth.org>
  16. *
  17. * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
  18. * <sjhill@realitydiluted.com>
  19. *
  20. * Copyright (C) 2007-2008 Marvell Semiconductor
  21. * Lennert Buytenhek <buytenh@marvell.com>
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version 2
  26. * of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/in.h>
  40. #include <linux/tcp.h>
  41. #include <linux/udp.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/delay.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/mii.h>
  51. #include <linux/mv643xx_eth.h>
  52. #include <asm/io.h>
  53. #include <asm/types.h>
  54. #include <asm/system.h>
  55. static char mv643xx_eth_driver_name[] = "mv643xx_eth";
  56. static char mv643xx_eth_driver_version[] = "1.3";
  57. #define MV643XX_ETH_CHECKSUM_OFFLOAD_TX
  58. #define MV643XX_ETH_TX_FAST_REFILL
  59. #ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
  60. #define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
  61. #else
  62. #define MAX_DESCS_PER_SKB 1
  63. #endif
  64. /*
  65. * Registers shared between all ports.
  66. */
  67. #define PHY_ADDR 0x0000
  68. #define SMI_REG 0x0004
  69. #define SMI_BUSY 0x10000000
  70. #define SMI_READ_VALID 0x08000000
  71. #define SMI_OPCODE_READ 0x04000000
  72. #define SMI_OPCODE_WRITE 0x00000000
  73. #define ERR_INT_CAUSE 0x0080
  74. #define ERR_INT_SMI_DONE 0x00000010
  75. #define ERR_INT_MASK 0x0084
  76. #define WINDOW_BASE(w) (0x0200 + ((w) << 3))
  77. #define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
  78. #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
  79. #define WINDOW_BAR_ENABLE 0x0290
  80. #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
  81. /*
  82. * Per-port registers.
  83. */
  84. #define PORT_CONFIG(p) (0x0400 + ((p) << 10))
  85. #define UNICAST_PROMISCUOUS_MODE 0x00000001
  86. #define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
  87. #define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
  88. #define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
  89. #define SDMA_CONFIG(p) (0x041c + ((p) << 10))
  90. #define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
  91. #define PORT_STATUS(p) (0x0444 + ((p) << 10))
  92. #define TX_FIFO_EMPTY 0x00000400
  93. #define TX_IN_PROGRESS 0x00000080
  94. #define PORT_SPEED_MASK 0x00000030
  95. #define PORT_SPEED_1000 0x00000010
  96. #define PORT_SPEED_100 0x00000020
  97. #define PORT_SPEED_10 0x00000000
  98. #define FLOW_CONTROL_ENABLED 0x00000008
  99. #define FULL_DUPLEX 0x00000004
  100. #define LINK_UP 0x00000002
  101. #define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
  102. #define TXQ_FIX_PRIO_CONF(p) (0x044c + ((p) << 10))
  103. #define TX_BW_RATE(p) (0x0450 + ((p) << 10))
  104. #define TX_BW_MTU(p) (0x0458 + ((p) << 10))
  105. #define TX_BW_BURST(p) (0x045c + ((p) << 10))
  106. #define INT_CAUSE(p) (0x0460 + ((p) << 10))
  107. #define INT_TX_END_0 0x00080000
  108. #define INT_TX_END 0x07f80000
  109. #define INT_RX 0x0007fbfc
  110. #define INT_EXT 0x00000002
  111. #define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
  112. #define INT_EXT_LINK 0x00100000
  113. #define INT_EXT_PHY 0x00010000
  114. #define INT_EXT_TX_ERROR_0 0x00000100
  115. #define INT_EXT_TX_0 0x00000001
  116. #define INT_EXT_TX 0x0000ffff
  117. #define INT_MASK(p) (0x0468 + ((p) << 10))
  118. #define INT_MASK_EXT(p) (0x046c + ((p) << 10))
  119. #define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
  120. #define TXQ_FIX_PRIO_CONF_MOVED(p) (0x04dc + ((p) << 10))
  121. #define TX_BW_RATE_MOVED(p) (0x04e0 + ((p) << 10))
  122. #define TX_BW_MTU_MOVED(p) (0x04e8 + ((p) << 10))
  123. #define TX_BW_BURST_MOVED(p) (0x04ec + ((p) << 10))
  124. #define RXQ_CURRENT_DESC_PTR(p, q) (0x060c + ((p) << 10) + ((q) << 4))
  125. #define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
  126. #define TXQ_CURRENT_DESC_PTR(p, q) (0x06c0 + ((p) << 10) + ((q) << 2))
  127. #define TXQ_BW_TOKENS(p, q) (0x0700 + ((p) << 10) + ((q) << 4))
  128. #define TXQ_BW_CONF(p, q) (0x0704 + ((p) << 10) + ((q) << 4))
  129. #define TXQ_BW_WRR_CONF(p, q) (0x0708 + ((p) << 10) + ((q) << 4))
  130. #define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
  131. #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
  132. #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
  133. #define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
  134. /*
  135. * SDMA configuration register.
  136. */
  137. #define RX_BURST_SIZE_16_64BIT (4 << 1)
  138. #define BLM_RX_NO_SWAP (1 << 4)
  139. #define BLM_TX_NO_SWAP (1 << 5)
  140. #define TX_BURST_SIZE_16_64BIT (4 << 22)
  141. #if defined(__BIG_ENDIAN)
  142. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  143. RX_BURST_SIZE_16_64BIT | \
  144. TX_BURST_SIZE_16_64BIT
  145. #elif defined(__LITTLE_ENDIAN)
  146. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  147. RX_BURST_SIZE_16_64BIT | \
  148. BLM_RX_NO_SWAP | \
  149. BLM_TX_NO_SWAP | \
  150. TX_BURST_SIZE_16_64BIT
  151. #else
  152. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  153. #endif
  154. /*
  155. * Port serial control register.
  156. */
  157. #define SET_MII_SPEED_TO_100 (1 << 24)
  158. #define SET_GMII_SPEED_TO_1000 (1 << 23)
  159. #define SET_FULL_DUPLEX_MODE (1 << 21)
  160. #define MAX_RX_PACKET_9700BYTE (5 << 17)
  161. #define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
  162. #define DO_NOT_FORCE_LINK_FAIL (1 << 10)
  163. #define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
  164. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
  165. #define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
  166. #define FORCE_LINK_PASS (1 << 1)
  167. #define SERIAL_PORT_ENABLE (1 << 0)
  168. #define DEFAULT_RX_QUEUE_SIZE 400
  169. #define DEFAULT_TX_QUEUE_SIZE 800
  170. /*
  171. * RX/TX descriptors.
  172. */
  173. #if defined(__BIG_ENDIAN)
  174. struct rx_desc {
  175. u16 byte_cnt; /* Descriptor buffer byte count */
  176. u16 buf_size; /* Buffer size */
  177. u32 cmd_sts; /* Descriptor command status */
  178. u32 next_desc_ptr; /* Next descriptor pointer */
  179. u32 buf_ptr; /* Descriptor buffer pointer */
  180. };
  181. struct tx_desc {
  182. u16 byte_cnt; /* buffer byte count */
  183. u16 l4i_chk; /* CPU provided TCP checksum */
  184. u32 cmd_sts; /* Command/status field */
  185. u32 next_desc_ptr; /* Pointer to next descriptor */
  186. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  187. };
  188. #elif defined(__LITTLE_ENDIAN)
  189. struct rx_desc {
  190. u32 cmd_sts; /* Descriptor command status */
  191. u16 buf_size; /* Buffer size */
  192. u16 byte_cnt; /* Descriptor buffer byte count */
  193. u32 buf_ptr; /* Descriptor buffer pointer */
  194. u32 next_desc_ptr; /* Next descriptor pointer */
  195. };
  196. struct tx_desc {
  197. u32 cmd_sts; /* Command/status field */
  198. u16 l4i_chk; /* CPU provided TCP checksum */
  199. u16 byte_cnt; /* buffer byte count */
  200. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  201. u32 next_desc_ptr; /* Pointer to next descriptor */
  202. };
  203. #else
  204. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  205. #endif
  206. /* RX & TX descriptor command */
  207. #define BUFFER_OWNED_BY_DMA 0x80000000
  208. /* RX & TX descriptor status */
  209. #define ERROR_SUMMARY 0x00000001
  210. /* RX descriptor status */
  211. #define LAYER_4_CHECKSUM_OK 0x40000000
  212. #define RX_ENABLE_INTERRUPT 0x20000000
  213. #define RX_FIRST_DESC 0x08000000
  214. #define RX_LAST_DESC 0x04000000
  215. /* TX descriptor command */
  216. #define TX_ENABLE_INTERRUPT 0x00800000
  217. #define GEN_CRC 0x00400000
  218. #define TX_FIRST_DESC 0x00200000
  219. #define TX_LAST_DESC 0x00100000
  220. #define ZERO_PADDING 0x00080000
  221. #define GEN_IP_V4_CHECKSUM 0x00040000
  222. #define GEN_TCP_UDP_CHECKSUM 0x00020000
  223. #define UDP_FRAME 0x00010000
  224. #define MAC_HDR_EXTRA_4_BYTES 0x00008000
  225. #define MAC_HDR_EXTRA_8_BYTES 0x00000200
  226. #define TX_IHL_SHIFT 11
  227. /* global *******************************************************************/
  228. struct mv643xx_eth_shared_private {
  229. /*
  230. * Ethernet controller base address.
  231. */
  232. void __iomem *base;
  233. /*
  234. * Protects access to SMI_REG, which is shared between ports.
  235. */
  236. struct mutex phy_lock;
  237. /*
  238. * If we have access to the error interrupt pin (which is
  239. * somewhat misnamed as it not only reflects internal errors
  240. * but also reflects SMI completion), use that to wait for
  241. * SMI access completion instead of polling the SMI busy bit.
  242. */
  243. int err_interrupt;
  244. wait_queue_head_t smi_busy_wait;
  245. /*
  246. * Per-port MBUS window access register value.
  247. */
  248. u32 win_protect;
  249. /*
  250. * Hardware-specific parameters.
  251. */
  252. unsigned int t_clk;
  253. int extended_rx_coal_limit;
  254. int tx_bw_control_moved;
  255. };
  256. /* per-port *****************************************************************/
  257. struct mib_counters {
  258. u64 good_octets_received;
  259. u32 bad_octets_received;
  260. u32 internal_mac_transmit_err;
  261. u32 good_frames_received;
  262. u32 bad_frames_received;
  263. u32 broadcast_frames_received;
  264. u32 multicast_frames_received;
  265. u32 frames_64_octets;
  266. u32 frames_65_to_127_octets;
  267. u32 frames_128_to_255_octets;
  268. u32 frames_256_to_511_octets;
  269. u32 frames_512_to_1023_octets;
  270. u32 frames_1024_to_max_octets;
  271. u64 good_octets_sent;
  272. u32 good_frames_sent;
  273. u32 excessive_collision;
  274. u32 multicast_frames_sent;
  275. u32 broadcast_frames_sent;
  276. u32 unrec_mac_control_received;
  277. u32 fc_sent;
  278. u32 good_fc_received;
  279. u32 bad_fc_received;
  280. u32 undersize_received;
  281. u32 fragments_received;
  282. u32 oversize_received;
  283. u32 jabber_received;
  284. u32 mac_receive_error;
  285. u32 bad_crc_event;
  286. u32 collision;
  287. u32 late_collision;
  288. };
  289. struct rx_queue {
  290. int index;
  291. int rx_ring_size;
  292. int rx_desc_count;
  293. int rx_curr_desc;
  294. int rx_used_desc;
  295. struct rx_desc *rx_desc_area;
  296. dma_addr_t rx_desc_dma;
  297. int rx_desc_area_size;
  298. struct sk_buff **rx_skb;
  299. };
  300. struct tx_queue {
  301. int index;
  302. int tx_ring_size;
  303. int tx_desc_count;
  304. int tx_curr_desc;
  305. int tx_used_desc;
  306. struct tx_desc *tx_desc_area;
  307. dma_addr_t tx_desc_dma;
  308. int tx_desc_area_size;
  309. struct sk_buff **tx_skb;
  310. };
  311. struct mv643xx_eth_private {
  312. struct mv643xx_eth_shared_private *shared;
  313. int port_num;
  314. struct net_device *dev;
  315. struct mv643xx_eth_shared_private *shared_smi;
  316. int phy_addr;
  317. spinlock_t lock;
  318. struct mib_counters mib_counters;
  319. struct work_struct tx_timeout_task;
  320. struct mii_if_info mii;
  321. /*
  322. * RX state.
  323. */
  324. int default_rx_ring_size;
  325. unsigned long rx_desc_sram_addr;
  326. int rx_desc_sram_size;
  327. u8 rxq_mask;
  328. int rxq_primary;
  329. struct napi_struct napi;
  330. struct timer_list rx_oom;
  331. struct rx_queue rxq[8];
  332. /*
  333. * TX state.
  334. */
  335. int default_tx_ring_size;
  336. unsigned long tx_desc_sram_addr;
  337. int tx_desc_sram_size;
  338. u8 txq_mask;
  339. int txq_primary;
  340. struct tx_queue txq[8];
  341. #ifdef MV643XX_ETH_TX_FAST_REFILL
  342. int tx_clean_threshold;
  343. #endif
  344. };
  345. /* port register accessors **************************************************/
  346. static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
  347. {
  348. return readl(mp->shared->base + offset);
  349. }
  350. static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
  351. {
  352. writel(data, mp->shared->base + offset);
  353. }
  354. /* rxq/txq helper functions *************************************************/
  355. static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
  356. {
  357. return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
  358. }
  359. static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
  360. {
  361. return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
  362. }
  363. static void rxq_enable(struct rx_queue *rxq)
  364. {
  365. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  366. wrl(mp, RXQ_COMMAND(mp->port_num), 1 << rxq->index);
  367. }
  368. static void rxq_disable(struct rx_queue *rxq)
  369. {
  370. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  371. u8 mask = 1 << rxq->index;
  372. wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
  373. while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
  374. udelay(10);
  375. }
  376. static void txq_reset_hw_ptr(struct tx_queue *txq)
  377. {
  378. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  379. int off = TXQ_CURRENT_DESC_PTR(mp->port_num, txq->index);
  380. u32 addr;
  381. addr = (u32)txq->tx_desc_dma;
  382. addr += txq->tx_curr_desc * sizeof(struct tx_desc);
  383. wrl(mp, off, addr);
  384. }
  385. static void txq_enable(struct tx_queue *txq)
  386. {
  387. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  388. wrl(mp, TXQ_COMMAND(mp->port_num), 1 << txq->index);
  389. }
  390. static void txq_disable(struct tx_queue *txq)
  391. {
  392. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  393. u8 mask = 1 << txq->index;
  394. wrl(mp, TXQ_COMMAND(mp->port_num), mask << 8);
  395. while (rdl(mp, TXQ_COMMAND(mp->port_num)) & mask)
  396. udelay(10);
  397. }
  398. static void __txq_maybe_wake(struct tx_queue *txq)
  399. {
  400. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  401. /*
  402. * netif_{stop,wake}_queue() flow control only applies to
  403. * the primary queue.
  404. */
  405. BUG_ON(txq->index != mp->txq_primary);
  406. if (txq->tx_ring_size - txq->tx_desc_count >= MAX_DESCS_PER_SKB)
  407. netif_wake_queue(mp->dev);
  408. }
  409. /* rx ***********************************************************************/
  410. static void txq_reclaim(struct tx_queue *txq, int force);
  411. static int rxq_refill(struct rx_queue *rxq, int budget, int *oom)
  412. {
  413. int skb_size;
  414. int refilled;
  415. /*
  416. * Reserve 2+14 bytes for an ethernet header (the hardware
  417. * automatically prepends 2 bytes of dummy data to each
  418. * received packet), 16 bytes for up to four VLAN tags, and
  419. * 4 bytes for the trailing FCS -- 36 bytes total.
  420. */
  421. skb_size = rxq_to_mp(rxq)->dev->mtu + 36;
  422. /*
  423. * Make sure that the skb size is a multiple of 8 bytes, as
  424. * the lower three bits of the receive descriptor's buffer
  425. * size field are ignored by the hardware.
  426. */
  427. skb_size = (skb_size + 7) & ~7;
  428. refilled = 0;
  429. while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
  430. struct sk_buff *skb;
  431. int unaligned;
  432. int rx;
  433. skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
  434. if (skb == NULL) {
  435. *oom = 1;
  436. break;
  437. }
  438. unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
  439. if (unaligned)
  440. skb_reserve(skb, dma_get_cache_alignment() - unaligned);
  441. refilled++;
  442. rxq->rx_desc_count++;
  443. rx = rxq->rx_used_desc++;
  444. if (rxq->rx_used_desc == rxq->rx_ring_size)
  445. rxq->rx_used_desc = 0;
  446. rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
  447. skb_size, DMA_FROM_DEVICE);
  448. rxq->rx_desc_area[rx].buf_size = skb_size;
  449. rxq->rx_skb[rx] = skb;
  450. wmb();
  451. rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
  452. RX_ENABLE_INTERRUPT;
  453. wmb();
  454. /*
  455. * The hardware automatically prepends 2 bytes of
  456. * dummy data to each received packet, so that the
  457. * IP header ends up 16-byte aligned.
  458. */
  459. skb_reserve(skb, 2);
  460. }
  461. return refilled;
  462. }
  463. static int rxq_process(struct rx_queue *rxq, int budget)
  464. {
  465. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  466. struct net_device_stats *stats = &mp->dev->stats;
  467. int rx;
  468. rx = 0;
  469. while (rx < budget && rxq->rx_desc_count) {
  470. struct rx_desc *rx_desc;
  471. unsigned int cmd_sts;
  472. struct sk_buff *skb;
  473. rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
  474. cmd_sts = rx_desc->cmd_sts;
  475. if (cmd_sts & BUFFER_OWNED_BY_DMA)
  476. break;
  477. rmb();
  478. skb = rxq->rx_skb[rxq->rx_curr_desc];
  479. rxq->rx_skb[rxq->rx_curr_desc] = NULL;
  480. rxq->rx_curr_desc++;
  481. if (rxq->rx_curr_desc == rxq->rx_ring_size)
  482. rxq->rx_curr_desc = 0;
  483. dma_unmap_single(NULL, rx_desc->buf_ptr,
  484. rx_desc->buf_size, DMA_FROM_DEVICE);
  485. rxq->rx_desc_count--;
  486. rx++;
  487. /*
  488. * Update statistics.
  489. *
  490. * Note that the descriptor byte count includes 2 dummy
  491. * bytes automatically inserted by the hardware at the
  492. * start of the packet (which we don't count), and a 4
  493. * byte CRC at the end of the packet (which we do count).
  494. */
  495. stats->rx_packets++;
  496. stats->rx_bytes += rx_desc->byte_cnt - 2;
  497. /*
  498. * In case we received a packet without first / last bits
  499. * on, or the error summary bit is set, the packet needs
  500. * to be dropped.
  501. */
  502. if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  503. (RX_FIRST_DESC | RX_LAST_DESC))
  504. || (cmd_sts & ERROR_SUMMARY)) {
  505. stats->rx_dropped++;
  506. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  507. (RX_FIRST_DESC | RX_LAST_DESC)) {
  508. if (net_ratelimit())
  509. dev_printk(KERN_ERR, &mp->dev->dev,
  510. "received packet spanning "
  511. "multiple descriptors\n");
  512. }
  513. if (cmd_sts & ERROR_SUMMARY)
  514. stats->rx_errors++;
  515. dev_kfree_skb(skb);
  516. } else {
  517. /*
  518. * The -4 is for the CRC in the trailer of the
  519. * received packet
  520. */
  521. skb_put(skb, rx_desc->byte_cnt - 2 - 4);
  522. if (cmd_sts & LAYER_4_CHECKSUM_OK) {
  523. skb->ip_summed = CHECKSUM_UNNECESSARY;
  524. skb->csum = htons(
  525. (cmd_sts & 0x0007fff8) >> 3);
  526. }
  527. skb->protocol = eth_type_trans(skb, mp->dev);
  528. netif_receive_skb(skb);
  529. }
  530. mp->dev->last_rx = jiffies;
  531. }
  532. return rx;
  533. }
  534. static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
  535. {
  536. struct mv643xx_eth_private *mp;
  537. int work_done;
  538. int oom;
  539. int i;
  540. mp = container_of(napi, struct mv643xx_eth_private, napi);
  541. #ifdef MV643XX_ETH_TX_FAST_REFILL
  542. if (++mp->tx_clean_threshold > 5) {
  543. mp->tx_clean_threshold = 0;
  544. for (i = 0; i < 8; i++)
  545. if (mp->txq_mask & (1 << i))
  546. txq_reclaim(mp->txq + i, 0);
  547. if (netif_carrier_ok(mp->dev)) {
  548. spin_lock_irq(&mp->lock);
  549. __txq_maybe_wake(mp->txq + mp->txq_primary);
  550. spin_unlock_irq(&mp->lock);
  551. }
  552. }
  553. #endif
  554. work_done = 0;
  555. oom = 0;
  556. for (i = 7; work_done < budget && i >= 0; i--) {
  557. if (mp->rxq_mask & (1 << i)) {
  558. struct rx_queue *rxq = mp->rxq + i;
  559. work_done += rxq_process(rxq, budget - work_done);
  560. work_done += rxq_refill(rxq, budget - work_done, &oom);
  561. }
  562. }
  563. if (work_done < budget) {
  564. if (oom)
  565. mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
  566. netif_rx_complete(mp->dev, napi);
  567. wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
  568. }
  569. return work_done;
  570. }
  571. static inline void oom_timer_wrapper(unsigned long data)
  572. {
  573. struct mv643xx_eth_private *mp = (void *)data;
  574. napi_schedule(&mp->napi);
  575. }
  576. /* tx ***********************************************************************/
  577. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  578. {
  579. int frag;
  580. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  581. skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  582. if (fragp->size <= 8 && fragp->page_offset & 7)
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. static int txq_alloc_desc_index(struct tx_queue *txq)
  588. {
  589. int tx_desc_curr;
  590. BUG_ON(txq->tx_desc_count >= txq->tx_ring_size);
  591. tx_desc_curr = txq->tx_curr_desc++;
  592. if (txq->tx_curr_desc == txq->tx_ring_size)
  593. txq->tx_curr_desc = 0;
  594. BUG_ON(txq->tx_curr_desc == txq->tx_used_desc);
  595. return tx_desc_curr;
  596. }
  597. static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
  598. {
  599. int nr_frags = skb_shinfo(skb)->nr_frags;
  600. int frag;
  601. for (frag = 0; frag < nr_frags; frag++) {
  602. skb_frag_t *this_frag;
  603. int tx_index;
  604. struct tx_desc *desc;
  605. this_frag = &skb_shinfo(skb)->frags[frag];
  606. tx_index = txq_alloc_desc_index(txq);
  607. desc = &txq->tx_desc_area[tx_index];
  608. /*
  609. * The last fragment will generate an interrupt
  610. * which will free the skb on TX completion.
  611. */
  612. if (frag == nr_frags - 1) {
  613. desc->cmd_sts = BUFFER_OWNED_BY_DMA |
  614. ZERO_PADDING | TX_LAST_DESC |
  615. TX_ENABLE_INTERRUPT;
  616. txq->tx_skb[tx_index] = skb;
  617. } else {
  618. desc->cmd_sts = BUFFER_OWNED_BY_DMA;
  619. txq->tx_skb[tx_index] = NULL;
  620. }
  621. desc->l4i_chk = 0;
  622. desc->byte_cnt = this_frag->size;
  623. desc->buf_ptr = dma_map_page(NULL, this_frag->page,
  624. this_frag->page_offset,
  625. this_frag->size,
  626. DMA_TO_DEVICE);
  627. }
  628. }
  629. static inline __be16 sum16_as_be(__sum16 sum)
  630. {
  631. return (__force __be16)sum;
  632. }
  633. static void txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
  634. {
  635. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  636. int nr_frags = skb_shinfo(skb)->nr_frags;
  637. int tx_index;
  638. struct tx_desc *desc;
  639. u32 cmd_sts;
  640. int length;
  641. cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
  642. tx_index = txq_alloc_desc_index(txq);
  643. desc = &txq->tx_desc_area[tx_index];
  644. if (nr_frags) {
  645. txq_submit_frag_skb(txq, skb);
  646. length = skb_headlen(skb);
  647. txq->tx_skb[tx_index] = NULL;
  648. } else {
  649. cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
  650. length = skb->len;
  651. txq->tx_skb[tx_index] = skb;
  652. }
  653. desc->byte_cnt = length;
  654. desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
  655. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  656. int mac_hdr_len;
  657. BUG_ON(skb->protocol != htons(ETH_P_IP) &&
  658. skb->protocol != htons(ETH_P_8021Q));
  659. cmd_sts |= GEN_TCP_UDP_CHECKSUM |
  660. GEN_IP_V4_CHECKSUM |
  661. ip_hdr(skb)->ihl << TX_IHL_SHIFT;
  662. mac_hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
  663. switch (mac_hdr_len - ETH_HLEN) {
  664. case 0:
  665. break;
  666. case 4:
  667. cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
  668. break;
  669. case 8:
  670. cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
  671. break;
  672. case 12:
  673. cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
  674. cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
  675. break;
  676. default:
  677. if (net_ratelimit())
  678. dev_printk(KERN_ERR, &txq_to_mp(txq)->dev->dev,
  679. "mac header length is %d?!\n", mac_hdr_len);
  680. break;
  681. }
  682. switch (ip_hdr(skb)->protocol) {
  683. case IPPROTO_UDP:
  684. cmd_sts |= UDP_FRAME;
  685. desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
  686. break;
  687. case IPPROTO_TCP:
  688. desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
  689. break;
  690. default:
  691. BUG();
  692. }
  693. } else {
  694. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  695. cmd_sts |= 5 << TX_IHL_SHIFT;
  696. desc->l4i_chk = 0;
  697. }
  698. /* ensure all other descriptors are written before first cmd_sts */
  699. wmb();
  700. desc->cmd_sts = cmd_sts;
  701. /* clear TX_END interrupt status */
  702. wrl(mp, INT_CAUSE(mp->port_num), ~(INT_TX_END_0 << txq->index));
  703. rdl(mp, INT_CAUSE(mp->port_num));
  704. /* ensure all descriptors are written before poking hardware */
  705. wmb();
  706. txq_enable(txq);
  707. txq->tx_desc_count += nr_frags + 1;
  708. }
  709. static int mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  710. {
  711. struct mv643xx_eth_private *mp = netdev_priv(dev);
  712. struct net_device_stats *stats = &dev->stats;
  713. struct tx_queue *txq;
  714. unsigned long flags;
  715. if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
  716. stats->tx_dropped++;
  717. dev_printk(KERN_DEBUG, &dev->dev,
  718. "failed to linearize skb with tiny "
  719. "unaligned fragment\n");
  720. return NETDEV_TX_BUSY;
  721. }
  722. spin_lock_irqsave(&mp->lock, flags);
  723. txq = mp->txq + mp->txq_primary;
  724. if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB) {
  725. spin_unlock_irqrestore(&mp->lock, flags);
  726. if (txq->index == mp->txq_primary && net_ratelimit())
  727. dev_printk(KERN_ERR, &dev->dev,
  728. "primary tx queue full?!\n");
  729. kfree_skb(skb);
  730. return NETDEV_TX_OK;
  731. }
  732. txq_submit_skb(txq, skb);
  733. stats->tx_bytes += skb->len;
  734. stats->tx_packets++;
  735. dev->trans_start = jiffies;
  736. if (txq->index == mp->txq_primary) {
  737. int entries_left;
  738. entries_left = txq->tx_ring_size - txq->tx_desc_count;
  739. if (entries_left < MAX_DESCS_PER_SKB)
  740. netif_stop_queue(dev);
  741. }
  742. spin_unlock_irqrestore(&mp->lock, flags);
  743. return NETDEV_TX_OK;
  744. }
  745. /* tx rate control **********************************************************/
  746. /*
  747. * Set total maximum TX rate (shared by all TX queues for this port)
  748. * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
  749. */
  750. static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
  751. {
  752. int token_rate;
  753. int mtu;
  754. int bucket_size;
  755. token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
  756. if (token_rate > 1023)
  757. token_rate = 1023;
  758. mtu = (mp->dev->mtu + 255) >> 8;
  759. if (mtu > 63)
  760. mtu = 63;
  761. bucket_size = (burst + 255) >> 8;
  762. if (bucket_size > 65535)
  763. bucket_size = 65535;
  764. if (mp->shared->tx_bw_control_moved) {
  765. wrl(mp, TX_BW_RATE_MOVED(mp->port_num), token_rate);
  766. wrl(mp, TX_BW_MTU_MOVED(mp->port_num), mtu);
  767. wrl(mp, TX_BW_BURST_MOVED(mp->port_num), bucket_size);
  768. } else {
  769. wrl(mp, TX_BW_RATE(mp->port_num), token_rate);
  770. wrl(mp, TX_BW_MTU(mp->port_num), mtu);
  771. wrl(mp, TX_BW_BURST(mp->port_num), bucket_size);
  772. }
  773. }
  774. static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
  775. {
  776. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  777. int token_rate;
  778. int bucket_size;
  779. token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
  780. if (token_rate > 1023)
  781. token_rate = 1023;
  782. bucket_size = (burst + 255) >> 8;
  783. if (bucket_size > 65535)
  784. bucket_size = 65535;
  785. wrl(mp, TXQ_BW_TOKENS(mp->port_num, txq->index), token_rate << 14);
  786. wrl(mp, TXQ_BW_CONF(mp->port_num, txq->index),
  787. (bucket_size << 10) | token_rate);
  788. }
  789. static void txq_set_fixed_prio_mode(struct tx_queue *txq)
  790. {
  791. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  792. int off;
  793. u32 val;
  794. /*
  795. * Turn on fixed priority mode.
  796. */
  797. if (mp->shared->tx_bw_control_moved)
  798. off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
  799. else
  800. off = TXQ_FIX_PRIO_CONF(mp->port_num);
  801. val = rdl(mp, off);
  802. val |= 1 << txq->index;
  803. wrl(mp, off, val);
  804. }
  805. static void txq_set_wrr(struct tx_queue *txq, int weight)
  806. {
  807. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  808. int off;
  809. u32 val;
  810. /*
  811. * Turn off fixed priority mode.
  812. */
  813. if (mp->shared->tx_bw_control_moved)
  814. off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
  815. else
  816. off = TXQ_FIX_PRIO_CONF(mp->port_num);
  817. val = rdl(mp, off);
  818. val &= ~(1 << txq->index);
  819. wrl(mp, off, val);
  820. /*
  821. * Configure WRR weight for this queue.
  822. */
  823. off = TXQ_BW_WRR_CONF(mp->port_num, txq->index);
  824. val = rdl(mp, off);
  825. val = (val & ~0xff) | (weight & 0xff);
  826. wrl(mp, off, val);
  827. }
  828. /* mii management interface *************************************************/
  829. static irqreturn_t mv643xx_eth_err_irq(int irq, void *dev_id)
  830. {
  831. struct mv643xx_eth_shared_private *msp = dev_id;
  832. if (readl(msp->base + ERR_INT_CAUSE) & ERR_INT_SMI_DONE) {
  833. writel(~ERR_INT_SMI_DONE, msp->base + ERR_INT_CAUSE);
  834. wake_up(&msp->smi_busy_wait);
  835. return IRQ_HANDLED;
  836. }
  837. return IRQ_NONE;
  838. }
  839. static int smi_is_done(struct mv643xx_eth_shared_private *msp)
  840. {
  841. return !(readl(msp->base + SMI_REG) & SMI_BUSY);
  842. }
  843. static int smi_wait_ready(struct mv643xx_eth_shared_private *msp)
  844. {
  845. if (msp->err_interrupt == NO_IRQ) {
  846. int i;
  847. for (i = 0; !smi_is_done(msp); i++) {
  848. if (i == 10)
  849. return -ETIMEDOUT;
  850. msleep(10);
  851. }
  852. return 0;
  853. }
  854. if (!wait_event_timeout(msp->smi_busy_wait, smi_is_done(msp),
  855. msecs_to_jiffies(100)))
  856. return -ETIMEDOUT;
  857. return 0;
  858. }
  859. static int smi_reg_read(struct mv643xx_eth_private *mp,
  860. unsigned int addr, unsigned int reg)
  861. {
  862. struct mv643xx_eth_shared_private *msp = mp->shared_smi;
  863. void __iomem *smi_reg = msp->base + SMI_REG;
  864. int ret;
  865. mutex_lock(&msp->phy_lock);
  866. if (smi_wait_ready(msp)) {
  867. printk("%s: SMI bus busy timeout\n", mp->dev->name);
  868. ret = -ETIMEDOUT;
  869. goto out;
  870. }
  871. writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
  872. if (smi_wait_ready(msp)) {
  873. printk("%s: SMI bus busy timeout\n", mp->dev->name);
  874. ret = -ETIMEDOUT;
  875. goto out;
  876. }
  877. ret = readl(smi_reg);
  878. if (!(ret & SMI_READ_VALID)) {
  879. printk("%s: SMI bus read not valid\n", mp->dev->name);
  880. ret = -ENODEV;
  881. goto out;
  882. }
  883. ret &= 0xffff;
  884. out:
  885. mutex_unlock(&msp->phy_lock);
  886. return ret;
  887. }
  888. static int smi_reg_write(struct mv643xx_eth_private *mp, unsigned int addr,
  889. unsigned int reg, unsigned int value)
  890. {
  891. struct mv643xx_eth_shared_private *msp = mp->shared_smi;
  892. void __iomem *smi_reg = msp->base + SMI_REG;
  893. mutex_lock(&msp->phy_lock);
  894. if (smi_wait_ready(msp)) {
  895. printk("%s: SMI bus busy timeout\n", mp->dev->name);
  896. mutex_unlock(&msp->phy_lock);
  897. return -ETIMEDOUT;
  898. }
  899. writel(SMI_OPCODE_WRITE | (reg << 21) |
  900. (addr << 16) | (value & 0xffff), smi_reg);
  901. mutex_unlock(&msp->phy_lock);
  902. return 0;
  903. }
  904. /* mib counters *************************************************************/
  905. static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
  906. {
  907. return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
  908. }
  909. static void mib_counters_clear(struct mv643xx_eth_private *mp)
  910. {
  911. int i;
  912. for (i = 0; i < 0x80; i += 4)
  913. mib_read(mp, i);
  914. }
  915. static void mib_counters_update(struct mv643xx_eth_private *mp)
  916. {
  917. struct mib_counters *p = &mp->mib_counters;
  918. p->good_octets_received += mib_read(mp, 0x00);
  919. p->good_octets_received += (u64)mib_read(mp, 0x04) << 32;
  920. p->bad_octets_received += mib_read(mp, 0x08);
  921. p->internal_mac_transmit_err += mib_read(mp, 0x0c);
  922. p->good_frames_received += mib_read(mp, 0x10);
  923. p->bad_frames_received += mib_read(mp, 0x14);
  924. p->broadcast_frames_received += mib_read(mp, 0x18);
  925. p->multicast_frames_received += mib_read(mp, 0x1c);
  926. p->frames_64_octets += mib_read(mp, 0x20);
  927. p->frames_65_to_127_octets += mib_read(mp, 0x24);
  928. p->frames_128_to_255_octets += mib_read(mp, 0x28);
  929. p->frames_256_to_511_octets += mib_read(mp, 0x2c);
  930. p->frames_512_to_1023_octets += mib_read(mp, 0x30);
  931. p->frames_1024_to_max_octets += mib_read(mp, 0x34);
  932. p->good_octets_sent += mib_read(mp, 0x38);
  933. p->good_octets_sent += (u64)mib_read(mp, 0x3c) << 32;
  934. p->good_frames_sent += mib_read(mp, 0x40);
  935. p->excessive_collision += mib_read(mp, 0x44);
  936. p->multicast_frames_sent += mib_read(mp, 0x48);
  937. p->broadcast_frames_sent += mib_read(mp, 0x4c);
  938. p->unrec_mac_control_received += mib_read(mp, 0x50);
  939. p->fc_sent += mib_read(mp, 0x54);
  940. p->good_fc_received += mib_read(mp, 0x58);
  941. p->bad_fc_received += mib_read(mp, 0x5c);
  942. p->undersize_received += mib_read(mp, 0x60);
  943. p->fragments_received += mib_read(mp, 0x64);
  944. p->oversize_received += mib_read(mp, 0x68);
  945. p->jabber_received += mib_read(mp, 0x6c);
  946. p->mac_receive_error += mib_read(mp, 0x70);
  947. p->bad_crc_event += mib_read(mp, 0x74);
  948. p->collision += mib_read(mp, 0x78);
  949. p->late_collision += mib_read(mp, 0x7c);
  950. }
  951. /* ethtool ******************************************************************/
  952. struct mv643xx_eth_stats {
  953. char stat_string[ETH_GSTRING_LEN];
  954. int sizeof_stat;
  955. int netdev_off;
  956. int mp_off;
  957. };
  958. #define SSTAT(m) \
  959. { #m, FIELD_SIZEOF(struct net_device_stats, m), \
  960. offsetof(struct net_device, stats.m), -1 }
  961. #define MIBSTAT(m) \
  962. { #m, FIELD_SIZEOF(struct mib_counters, m), \
  963. -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
  964. static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
  965. SSTAT(rx_packets),
  966. SSTAT(tx_packets),
  967. SSTAT(rx_bytes),
  968. SSTAT(tx_bytes),
  969. SSTAT(rx_errors),
  970. SSTAT(tx_errors),
  971. SSTAT(rx_dropped),
  972. SSTAT(tx_dropped),
  973. MIBSTAT(good_octets_received),
  974. MIBSTAT(bad_octets_received),
  975. MIBSTAT(internal_mac_transmit_err),
  976. MIBSTAT(good_frames_received),
  977. MIBSTAT(bad_frames_received),
  978. MIBSTAT(broadcast_frames_received),
  979. MIBSTAT(multicast_frames_received),
  980. MIBSTAT(frames_64_octets),
  981. MIBSTAT(frames_65_to_127_octets),
  982. MIBSTAT(frames_128_to_255_octets),
  983. MIBSTAT(frames_256_to_511_octets),
  984. MIBSTAT(frames_512_to_1023_octets),
  985. MIBSTAT(frames_1024_to_max_octets),
  986. MIBSTAT(good_octets_sent),
  987. MIBSTAT(good_frames_sent),
  988. MIBSTAT(excessive_collision),
  989. MIBSTAT(multicast_frames_sent),
  990. MIBSTAT(broadcast_frames_sent),
  991. MIBSTAT(unrec_mac_control_received),
  992. MIBSTAT(fc_sent),
  993. MIBSTAT(good_fc_received),
  994. MIBSTAT(bad_fc_received),
  995. MIBSTAT(undersize_received),
  996. MIBSTAT(fragments_received),
  997. MIBSTAT(oversize_received),
  998. MIBSTAT(jabber_received),
  999. MIBSTAT(mac_receive_error),
  1000. MIBSTAT(bad_crc_event),
  1001. MIBSTAT(collision),
  1002. MIBSTAT(late_collision),
  1003. };
  1004. static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1005. {
  1006. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1007. int err;
  1008. err = mii_ethtool_gset(&mp->mii, cmd);
  1009. /*
  1010. * The MAC does not support 1000baseT_Half.
  1011. */
  1012. cmd->supported &= ~SUPPORTED_1000baseT_Half;
  1013. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  1014. return err;
  1015. }
  1016. static int mv643xx_eth_get_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
  1017. {
  1018. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1019. u32 port_status;
  1020. port_status = rdl(mp, PORT_STATUS(mp->port_num));
  1021. cmd->supported = SUPPORTED_MII;
  1022. cmd->advertising = ADVERTISED_MII;
  1023. switch (port_status & PORT_SPEED_MASK) {
  1024. case PORT_SPEED_10:
  1025. cmd->speed = SPEED_10;
  1026. break;
  1027. case PORT_SPEED_100:
  1028. cmd->speed = SPEED_100;
  1029. break;
  1030. case PORT_SPEED_1000:
  1031. cmd->speed = SPEED_1000;
  1032. break;
  1033. default:
  1034. cmd->speed = -1;
  1035. break;
  1036. }
  1037. cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
  1038. cmd->port = PORT_MII;
  1039. cmd->phy_address = 0;
  1040. cmd->transceiver = XCVR_INTERNAL;
  1041. cmd->autoneg = AUTONEG_DISABLE;
  1042. cmd->maxtxpkt = 1;
  1043. cmd->maxrxpkt = 1;
  1044. return 0;
  1045. }
  1046. static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1047. {
  1048. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1049. /*
  1050. * The MAC does not support 1000baseT_Half.
  1051. */
  1052. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  1053. return mii_ethtool_sset(&mp->mii, cmd);
  1054. }
  1055. static int mv643xx_eth_set_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
  1056. {
  1057. return -EINVAL;
  1058. }
  1059. static void mv643xx_eth_get_drvinfo(struct net_device *dev,
  1060. struct ethtool_drvinfo *drvinfo)
  1061. {
  1062. strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
  1063. strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
  1064. strncpy(drvinfo->fw_version, "N/A", 32);
  1065. strncpy(drvinfo->bus_info, "platform", 32);
  1066. drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
  1067. }
  1068. static int mv643xx_eth_nway_reset(struct net_device *dev)
  1069. {
  1070. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1071. return mii_nway_restart(&mp->mii);
  1072. }
  1073. static int mv643xx_eth_nway_reset_phyless(struct net_device *dev)
  1074. {
  1075. return -EINVAL;
  1076. }
  1077. static u32 mv643xx_eth_get_link(struct net_device *dev)
  1078. {
  1079. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1080. return mii_link_ok(&mp->mii);
  1081. }
  1082. static u32 mv643xx_eth_get_link_phyless(struct net_device *dev)
  1083. {
  1084. return 1;
  1085. }
  1086. static void mv643xx_eth_get_strings(struct net_device *dev,
  1087. uint32_t stringset, uint8_t *data)
  1088. {
  1089. int i;
  1090. if (stringset == ETH_SS_STATS) {
  1091. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1092. memcpy(data + i * ETH_GSTRING_LEN,
  1093. mv643xx_eth_stats[i].stat_string,
  1094. ETH_GSTRING_LEN);
  1095. }
  1096. }
  1097. }
  1098. static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
  1099. struct ethtool_stats *stats,
  1100. uint64_t *data)
  1101. {
  1102. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1103. int i;
  1104. mib_counters_update(mp);
  1105. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1106. const struct mv643xx_eth_stats *stat;
  1107. void *p;
  1108. stat = mv643xx_eth_stats + i;
  1109. if (stat->netdev_off >= 0)
  1110. p = ((void *)mp->dev) + stat->netdev_off;
  1111. else
  1112. p = ((void *)mp) + stat->mp_off;
  1113. data[i] = (stat->sizeof_stat == 8) ?
  1114. *(uint64_t *)p : *(uint32_t *)p;
  1115. }
  1116. }
  1117. static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
  1118. {
  1119. if (sset == ETH_SS_STATS)
  1120. return ARRAY_SIZE(mv643xx_eth_stats);
  1121. return -EOPNOTSUPP;
  1122. }
  1123. static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
  1124. .get_settings = mv643xx_eth_get_settings,
  1125. .set_settings = mv643xx_eth_set_settings,
  1126. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1127. .nway_reset = mv643xx_eth_nway_reset,
  1128. .get_link = mv643xx_eth_get_link,
  1129. .set_sg = ethtool_op_set_sg,
  1130. .get_strings = mv643xx_eth_get_strings,
  1131. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1132. .get_sset_count = mv643xx_eth_get_sset_count,
  1133. };
  1134. static const struct ethtool_ops mv643xx_eth_ethtool_ops_phyless = {
  1135. .get_settings = mv643xx_eth_get_settings_phyless,
  1136. .set_settings = mv643xx_eth_set_settings_phyless,
  1137. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1138. .nway_reset = mv643xx_eth_nway_reset_phyless,
  1139. .get_link = mv643xx_eth_get_link_phyless,
  1140. .set_sg = ethtool_op_set_sg,
  1141. .get_strings = mv643xx_eth_get_strings,
  1142. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1143. .get_sset_count = mv643xx_eth_get_sset_count,
  1144. };
  1145. /* address handling *********************************************************/
  1146. static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
  1147. {
  1148. unsigned int mac_h;
  1149. unsigned int mac_l;
  1150. mac_h = rdl(mp, MAC_ADDR_HIGH(mp->port_num));
  1151. mac_l = rdl(mp, MAC_ADDR_LOW(mp->port_num));
  1152. addr[0] = (mac_h >> 24) & 0xff;
  1153. addr[1] = (mac_h >> 16) & 0xff;
  1154. addr[2] = (mac_h >> 8) & 0xff;
  1155. addr[3] = mac_h & 0xff;
  1156. addr[4] = (mac_l >> 8) & 0xff;
  1157. addr[5] = mac_l & 0xff;
  1158. }
  1159. static void init_mac_tables(struct mv643xx_eth_private *mp)
  1160. {
  1161. int i;
  1162. for (i = 0; i < 0x100; i += 4) {
  1163. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
  1164. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
  1165. }
  1166. for (i = 0; i < 0x10; i += 4)
  1167. wrl(mp, UNICAST_TABLE(mp->port_num) + i, 0);
  1168. }
  1169. static void set_filter_table_entry(struct mv643xx_eth_private *mp,
  1170. int table, unsigned char entry)
  1171. {
  1172. unsigned int table_reg;
  1173. /* Set "accepts frame bit" at specified table entry */
  1174. table_reg = rdl(mp, table + (entry & 0xfc));
  1175. table_reg |= 0x01 << (8 * (entry & 3));
  1176. wrl(mp, table + (entry & 0xfc), table_reg);
  1177. }
  1178. static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
  1179. {
  1180. unsigned int mac_h;
  1181. unsigned int mac_l;
  1182. int table;
  1183. mac_l = (addr[4] << 8) | addr[5];
  1184. mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  1185. wrl(mp, MAC_ADDR_LOW(mp->port_num), mac_l);
  1186. wrl(mp, MAC_ADDR_HIGH(mp->port_num), mac_h);
  1187. table = UNICAST_TABLE(mp->port_num);
  1188. set_filter_table_entry(mp, table, addr[5] & 0x0f);
  1189. }
  1190. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  1191. {
  1192. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1193. /* +2 is for the offset of the HW addr type */
  1194. memcpy(dev->dev_addr, addr + 2, 6);
  1195. init_mac_tables(mp);
  1196. uc_addr_set(mp, dev->dev_addr);
  1197. return 0;
  1198. }
  1199. static int addr_crc(unsigned char *addr)
  1200. {
  1201. int crc = 0;
  1202. int i;
  1203. for (i = 0; i < 6; i++) {
  1204. int j;
  1205. crc = (crc ^ addr[i]) << 8;
  1206. for (j = 7; j >= 0; j--) {
  1207. if (crc & (0x100 << j))
  1208. crc ^= 0x107 << j;
  1209. }
  1210. }
  1211. return crc;
  1212. }
  1213. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  1214. {
  1215. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1216. u32 port_config;
  1217. struct dev_addr_list *addr;
  1218. int i;
  1219. port_config = rdl(mp, PORT_CONFIG(mp->port_num));
  1220. if (dev->flags & IFF_PROMISC)
  1221. port_config |= UNICAST_PROMISCUOUS_MODE;
  1222. else
  1223. port_config &= ~UNICAST_PROMISCUOUS_MODE;
  1224. wrl(mp, PORT_CONFIG(mp->port_num), port_config);
  1225. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  1226. int port_num = mp->port_num;
  1227. u32 accept = 0x01010101;
  1228. for (i = 0; i < 0x100; i += 4) {
  1229. wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
  1230. wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
  1231. }
  1232. return;
  1233. }
  1234. for (i = 0; i < 0x100; i += 4) {
  1235. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
  1236. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
  1237. }
  1238. for (addr = dev->mc_list; addr != NULL; addr = addr->next) {
  1239. u8 *a = addr->da_addr;
  1240. int table;
  1241. if (addr->da_addrlen != 6)
  1242. continue;
  1243. if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
  1244. table = SPECIAL_MCAST_TABLE(mp->port_num);
  1245. set_filter_table_entry(mp, table, a[5]);
  1246. } else {
  1247. int crc = addr_crc(a);
  1248. table = OTHER_MCAST_TABLE(mp->port_num);
  1249. set_filter_table_entry(mp, table, crc);
  1250. }
  1251. }
  1252. }
  1253. /* rx/tx queue initialisation ***********************************************/
  1254. static int rxq_init(struct mv643xx_eth_private *mp, int index)
  1255. {
  1256. struct rx_queue *rxq = mp->rxq + index;
  1257. struct rx_desc *rx_desc;
  1258. int size;
  1259. int i;
  1260. rxq->index = index;
  1261. rxq->rx_ring_size = mp->default_rx_ring_size;
  1262. rxq->rx_desc_count = 0;
  1263. rxq->rx_curr_desc = 0;
  1264. rxq->rx_used_desc = 0;
  1265. size = rxq->rx_ring_size * sizeof(struct rx_desc);
  1266. if (index == mp->rxq_primary && size <= mp->rx_desc_sram_size) {
  1267. rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
  1268. mp->rx_desc_sram_size);
  1269. rxq->rx_desc_dma = mp->rx_desc_sram_addr;
  1270. } else {
  1271. rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
  1272. &rxq->rx_desc_dma,
  1273. GFP_KERNEL);
  1274. }
  1275. if (rxq->rx_desc_area == NULL) {
  1276. dev_printk(KERN_ERR, &mp->dev->dev,
  1277. "can't allocate rx ring (%d bytes)\n", size);
  1278. goto out;
  1279. }
  1280. memset(rxq->rx_desc_area, 0, size);
  1281. rxq->rx_desc_area_size = size;
  1282. rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
  1283. GFP_KERNEL);
  1284. if (rxq->rx_skb == NULL) {
  1285. dev_printk(KERN_ERR, &mp->dev->dev,
  1286. "can't allocate rx skb ring\n");
  1287. goto out_free;
  1288. }
  1289. rx_desc = (struct rx_desc *)rxq->rx_desc_area;
  1290. for (i = 0; i < rxq->rx_ring_size; i++) {
  1291. int nexti;
  1292. nexti = i + 1;
  1293. if (nexti == rxq->rx_ring_size)
  1294. nexti = 0;
  1295. rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
  1296. nexti * sizeof(struct rx_desc);
  1297. }
  1298. return 0;
  1299. out_free:
  1300. if (index == mp->rxq_primary && size <= mp->rx_desc_sram_size)
  1301. iounmap(rxq->rx_desc_area);
  1302. else
  1303. dma_free_coherent(NULL, size,
  1304. rxq->rx_desc_area,
  1305. rxq->rx_desc_dma);
  1306. out:
  1307. return -ENOMEM;
  1308. }
  1309. static void rxq_deinit(struct rx_queue *rxq)
  1310. {
  1311. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  1312. int i;
  1313. rxq_disable(rxq);
  1314. for (i = 0; i < rxq->rx_ring_size; i++) {
  1315. if (rxq->rx_skb[i]) {
  1316. dev_kfree_skb(rxq->rx_skb[i]);
  1317. rxq->rx_desc_count--;
  1318. }
  1319. }
  1320. if (rxq->rx_desc_count) {
  1321. dev_printk(KERN_ERR, &mp->dev->dev,
  1322. "error freeing rx ring -- %d skbs stuck\n",
  1323. rxq->rx_desc_count);
  1324. }
  1325. if (rxq->index == mp->rxq_primary &&
  1326. rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
  1327. iounmap(rxq->rx_desc_area);
  1328. else
  1329. dma_free_coherent(NULL, rxq->rx_desc_area_size,
  1330. rxq->rx_desc_area, rxq->rx_desc_dma);
  1331. kfree(rxq->rx_skb);
  1332. }
  1333. static int txq_init(struct mv643xx_eth_private *mp, int index)
  1334. {
  1335. struct tx_queue *txq = mp->txq + index;
  1336. struct tx_desc *tx_desc;
  1337. int size;
  1338. int i;
  1339. txq->index = index;
  1340. txq->tx_ring_size = mp->default_tx_ring_size;
  1341. txq->tx_desc_count = 0;
  1342. txq->tx_curr_desc = 0;
  1343. txq->tx_used_desc = 0;
  1344. size = txq->tx_ring_size * sizeof(struct tx_desc);
  1345. if (index == mp->txq_primary && size <= mp->tx_desc_sram_size) {
  1346. txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
  1347. mp->tx_desc_sram_size);
  1348. txq->tx_desc_dma = mp->tx_desc_sram_addr;
  1349. } else {
  1350. txq->tx_desc_area = dma_alloc_coherent(NULL, size,
  1351. &txq->tx_desc_dma,
  1352. GFP_KERNEL);
  1353. }
  1354. if (txq->tx_desc_area == NULL) {
  1355. dev_printk(KERN_ERR, &mp->dev->dev,
  1356. "can't allocate tx ring (%d bytes)\n", size);
  1357. goto out;
  1358. }
  1359. memset(txq->tx_desc_area, 0, size);
  1360. txq->tx_desc_area_size = size;
  1361. txq->tx_skb = kmalloc(txq->tx_ring_size * sizeof(*txq->tx_skb),
  1362. GFP_KERNEL);
  1363. if (txq->tx_skb == NULL) {
  1364. dev_printk(KERN_ERR, &mp->dev->dev,
  1365. "can't allocate tx skb ring\n");
  1366. goto out_free;
  1367. }
  1368. tx_desc = (struct tx_desc *)txq->tx_desc_area;
  1369. for (i = 0; i < txq->tx_ring_size; i++) {
  1370. struct tx_desc *txd = tx_desc + i;
  1371. int nexti;
  1372. nexti = i + 1;
  1373. if (nexti == txq->tx_ring_size)
  1374. nexti = 0;
  1375. txd->cmd_sts = 0;
  1376. txd->next_desc_ptr = txq->tx_desc_dma +
  1377. nexti * sizeof(struct tx_desc);
  1378. }
  1379. return 0;
  1380. out_free:
  1381. if (index == mp->txq_primary && size <= mp->tx_desc_sram_size)
  1382. iounmap(txq->tx_desc_area);
  1383. else
  1384. dma_free_coherent(NULL, size,
  1385. txq->tx_desc_area,
  1386. txq->tx_desc_dma);
  1387. out:
  1388. return -ENOMEM;
  1389. }
  1390. static void txq_reclaim(struct tx_queue *txq, int force)
  1391. {
  1392. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1393. unsigned long flags;
  1394. spin_lock_irqsave(&mp->lock, flags);
  1395. while (txq->tx_desc_count > 0) {
  1396. int tx_index;
  1397. struct tx_desc *desc;
  1398. u32 cmd_sts;
  1399. struct sk_buff *skb;
  1400. dma_addr_t addr;
  1401. int count;
  1402. tx_index = txq->tx_used_desc;
  1403. desc = &txq->tx_desc_area[tx_index];
  1404. cmd_sts = desc->cmd_sts;
  1405. if (cmd_sts & BUFFER_OWNED_BY_DMA) {
  1406. if (!force)
  1407. break;
  1408. desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
  1409. }
  1410. txq->tx_used_desc = tx_index + 1;
  1411. if (txq->tx_used_desc == txq->tx_ring_size)
  1412. txq->tx_used_desc = 0;
  1413. txq->tx_desc_count--;
  1414. addr = desc->buf_ptr;
  1415. count = desc->byte_cnt;
  1416. skb = txq->tx_skb[tx_index];
  1417. txq->tx_skb[tx_index] = NULL;
  1418. if (cmd_sts & ERROR_SUMMARY) {
  1419. dev_printk(KERN_INFO, &mp->dev->dev, "tx error\n");
  1420. mp->dev->stats.tx_errors++;
  1421. }
  1422. /*
  1423. * Drop mp->lock while we free the skb.
  1424. */
  1425. spin_unlock_irqrestore(&mp->lock, flags);
  1426. if (cmd_sts & TX_FIRST_DESC)
  1427. dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
  1428. else
  1429. dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
  1430. if (skb)
  1431. dev_kfree_skb_irq(skb);
  1432. spin_lock_irqsave(&mp->lock, flags);
  1433. }
  1434. spin_unlock_irqrestore(&mp->lock, flags);
  1435. }
  1436. static void txq_deinit(struct tx_queue *txq)
  1437. {
  1438. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1439. txq_disable(txq);
  1440. txq_reclaim(txq, 1);
  1441. BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
  1442. if (txq->index == mp->txq_primary &&
  1443. txq->tx_desc_area_size <= mp->tx_desc_sram_size)
  1444. iounmap(txq->tx_desc_area);
  1445. else
  1446. dma_free_coherent(NULL, txq->tx_desc_area_size,
  1447. txq->tx_desc_area, txq->tx_desc_dma);
  1448. kfree(txq->tx_skb);
  1449. }
  1450. /* netdev ops and related ***************************************************/
  1451. static void handle_link_event(struct mv643xx_eth_private *mp)
  1452. {
  1453. struct net_device *dev = mp->dev;
  1454. u32 port_status;
  1455. int speed;
  1456. int duplex;
  1457. int fc;
  1458. port_status = rdl(mp, PORT_STATUS(mp->port_num));
  1459. if (!(port_status & LINK_UP)) {
  1460. if (netif_carrier_ok(dev)) {
  1461. int i;
  1462. printk(KERN_INFO "%s: link down\n", dev->name);
  1463. netif_carrier_off(dev);
  1464. netif_stop_queue(dev);
  1465. for (i = 0; i < 8; i++) {
  1466. struct tx_queue *txq = mp->txq + i;
  1467. if (mp->txq_mask & (1 << i)) {
  1468. txq_reclaim(txq, 1);
  1469. txq_reset_hw_ptr(txq);
  1470. }
  1471. }
  1472. }
  1473. return;
  1474. }
  1475. switch (port_status & PORT_SPEED_MASK) {
  1476. case PORT_SPEED_10:
  1477. speed = 10;
  1478. break;
  1479. case PORT_SPEED_100:
  1480. speed = 100;
  1481. break;
  1482. case PORT_SPEED_1000:
  1483. speed = 1000;
  1484. break;
  1485. default:
  1486. speed = -1;
  1487. break;
  1488. }
  1489. duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
  1490. fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
  1491. printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
  1492. "flow control %sabled\n", dev->name,
  1493. speed, duplex ? "full" : "half",
  1494. fc ? "en" : "dis");
  1495. if (!netif_carrier_ok(dev)) {
  1496. netif_carrier_on(dev);
  1497. netif_wake_queue(dev);
  1498. }
  1499. }
  1500. static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
  1501. {
  1502. struct net_device *dev = (struct net_device *)dev_id;
  1503. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1504. u32 int_cause;
  1505. u32 int_cause_ext;
  1506. int_cause = rdl(mp, INT_CAUSE(mp->port_num)) &
  1507. (INT_TX_END | INT_RX | INT_EXT);
  1508. if (int_cause == 0)
  1509. return IRQ_NONE;
  1510. int_cause_ext = 0;
  1511. if (int_cause & INT_EXT) {
  1512. int_cause_ext = rdl(mp, INT_CAUSE_EXT(mp->port_num))
  1513. & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
  1514. wrl(mp, INT_CAUSE_EXT(mp->port_num), ~int_cause_ext);
  1515. }
  1516. if (int_cause_ext & (INT_EXT_PHY | INT_EXT_LINK))
  1517. handle_link_event(mp);
  1518. /*
  1519. * RxBuffer or RxError set for any of the 8 queues?
  1520. */
  1521. if (int_cause & INT_RX) {
  1522. wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_RX));
  1523. wrl(mp, INT_MASK(mp->port_num), 0x00000000);
  1524. rdl(mp, INT_MASK(mp->port_num));
  1525. napi_schedule(&mp->napi);
  1526. }
  1527. /*
  1528. * TxBuffer or TxError set for any of the 8 queues?
  1529. */
  1530. if (int_cause_ext & INT_EXT_TX) {
  1531. int i;
  1532. for (i = 0; i < 8; i++)
  1533. if (mp->txq_mask & (1 << i))
  1534. txq_reclaim(mp->txq + i, 0);
  1535. /*
  1536. * Enough space again in the primary TX queue for a
  1537. * full packet?
  1538. */
  1539. if (netif_carrier_ok(dev)) {
  1540. spin_lock(&mp->lock);
  1541. __txq_maybe_wake(mp->txq + mp->txq_primary);
  1542. spin_unlock(&mp->lock);
  1543. }
  1544. }
  1545. /*
  1546. * Any TxEnd interrupts?
  1547. */
  1548. if (int_cause & INT_TX_END) {
  1549. int i;
  1550. wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_TX_END));
  1551. spin_lock(&mp->lock);
  1552. for (i = 0; i < 8; i++) {
  1553. struct tx_queue *txq = mp->txq + i;
  1554. u32 hw_desc_ptr;
  1555. u32 expected_ptr;
  1556. if ((int_cause & (INT_TX_END_0 << i)) == 0)
  1557. continue;
  1558. hw_desc_ptr =
  1559. rdl(mp, TXQ_CURRENT_DESC_PTR(mp->port_num, i));
  1560. expected_ptr = (u32)txq->tx_desc_dma +
  1561. txq->tx_curr_desc * sizeof(struct tx_desc);
  1562. if (hw_desc_ptr != expected_ptr)
  1563. txq_enable(txq);
  1564. }
  1565. spin_unlock(&mp->lock);
  1566. }
  1567. return IRQ_HANDLED;
  1568. }
  1569. static void phy_reset(struct mv643xx_eth_private *mp)
  1570. {
  1571. int data;
  1572. data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
  1573. if (data < 0)
  1574. return;
  1575. data |= BMCR_RESET;
  1576. if (smi_reg_write(mp, mp->phy_addr, MII_BMCR, data) < 0)
  1577. return;
  1578. do {
  1579. data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
  1580. } while (data >= 0 && data & BMCR_RESET);
  1581. }
  1582. static void port_start(struct mv643xx_eth_private *mp)
  1583. {
  1584. u32 pscr;
  1585. int i;
  1586. /*
  1587. * Perform PHY reset, if there is a PHY.
  1588. */
  1589. if (mp->phy_addr != -1) {
  1590. struct ethtool_cmd cmd;
  1591. mv643xx_eth_get_settings(mp->dev, &cmd);
  1592. phy_reset(mp);
  1593. mv643xx_eth_set_settings(mp->dev, &cmd);
  1594. }
  1595. /*
  1596. * Configure basic link parameters.
  1597. */
  1598. pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
  1599. pscr |= SERIAL_PORT_ENABLE;
  1600. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  1601. pscr |= DO_NOT_FORCE_LINK_FAIL;
  1602. if (mp->phy_addr == -1)
  1603. pscr |= FORCE_LINK_PASS;
  1604. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  1605. wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
  1606. /*
  1607. * Configure TX path and queues.
  1608. */
  1609. tx_set_rate(mp, 1000000000, 16777216);
  1610. for (i = 0; i < 8; i++) {
  1611. struct tx_queue *txq = mp->txq + i;
  1612. if ((mp->txq_mask & (1 << i)) == 0)
  1613. continue;
  1614. txq_reset_hw_ptr(txq);
  1615. txq_set_rate(txq, 1000000000, 16777216);
  1616. txq_set_fixed_prio_mode(txq);
  1617. }
  1618. /*
  1619. * Add configured unicast address to address filter table.
  1620. */
  1621. uc_addr_set(mp, mp->dev->dev_addr);
  1622. /*
  1623. * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
  1624. * frames to RX queue #0.
  1625. */
  1626. wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
  1627. /*
  1628. * Treat BPDUs as normal multicasts, and disable partition mode.
  1629. */
  1630. wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
  1631. /*
  1632. * Enable the receive queues.
  1633. */
  1634. for (i = 0; i < 8; i++) {
  1635. struct rx_queue *rxq = mp->rxq + i;
  1636. int off = RXQ_CURRENT_DESC_PTR(mp->port_num, i);
  1637. u32 addr;
  1638. if ((mp->rxq_mask & (1 << i)) == 0)
  1639. continue;
  1640. addr = (u32)rxq->rx_desc_dma;
  1641. addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
  1642. wrl(mp, off, addr);
  1643. rxq_enable(rxq);
  1644. }
  1645. }
  1646. static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
  1647. {
  1648. unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
  1649. u32 val;
  1650. val = rdl(mp, SDMA_CONFIG(mp->port_num));
  1651. if (mp->shared->extended_rx_coal_limit) {
  1652. if (coal > 0xffff)
  1653. coal = 0xffff;
  1654. val &= ~0x023fff80;
  1655. val |= (coal & 0x8000) << 10;
  1656. val |= (coal & 0x7fff) << 7;
  1657. } else {
  1658. if (coal > 0x3fff)
  1659. coal = 0x3fff;
  1660. val &= ~0x003fff00;
  1661. val |= (coal & 0x3fff) << 8;
  1662. }
  1663. wrl(mp, SDMA_CONFIG(mp->port_num), val);
  1664. }
  1665. static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
  1666. {
  1667. unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
  1668. if (coal > 0x3fff)
  1669. coal = 0x3fff;
  1670. wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), (coal & 0x3fff) << 4);
  1671. }
  1672. static int mv643xx_eth_open(struct net_device *dev)
  1673. {
  1674. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1675. int err;
  1676. int oom;
  1677. int i;
  1678. wrl(mp, INT_CAUSE(mp->port_num), 0);
  1679. wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
  1680. rdl(mp, INT_CAUSE_EXT(mp->port_num));
  1681. err = request_irq(dev->irq, mv643xx_eth_irq,
  1682. IRQF_SHARED, dev->name, dev);
  1683. if (err) {
  1684. dev_printk(KERN_ERR, &dev->dev, "can't assign irq\n");
  1685. return -EAGAIN;
  1686. }
  1687. init_mac_tables(mp);
  1688. napi_enable(&mp->napi);
  1689. oom = 0;
  1690. for (i = 0; i < 8; i++) {
  1691. if ((mp->rxq_mask & (1 << i)) == 0)
  1692. continue;
  1693. err = rxq_init(mp, i);
  1694. if (err) {
  1695. while (--i >= 0)
  1696. if (mp->rxq_mask & (1 << i))
  1697. rxq_deinit(mp->rxq + i);
  1698. goto out;
  1699. }
  1700. rxq_refill(mp->rxq + i, INT_MAX, &oom);
  1701. }
  1702. if (oom) {
  1703. mp->rx_oom.expires = jiffies + (HZ / 10);
  1704. add_timer(&mp->rx_oom);
  1705. }
  1706. for (i = 0; i < 8; i++) {
  1707. if ((mp->txq_mask & (1 << i)) == 0)
  1708. continue;
  1709. err = txq_init(mp, i);
  1710. if (err) {
  1711. while (--i >= 0)
  1712. if (mp->txq_mask & (1 << i))
  1713. txq_deinit(mp->txq + i);
  1714. goto out_free;
  1715. }
  1716. }
  1717. netif_carrier_off(dev);
  1718. netif_stop_queue(dev);
  1719. port_start(mp);
  1720. set_rx_coal(mp, 0);
  1721. set_tx_coal(mp, 0);
  1722. wrl(mp, INT_MASK_EXT(mp->port_num),
  1723. INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
  1724. wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
  1725. return 0;
  1726. out_free:
  1727. for (i = 0; i < 8; i++)
  1728. if (mp->rxq_mask & (1 << i))
  1729. rxq_deinit(mp->rxq + i);
  1730. out:
  1731. free_irq(dev->irq, dev);
  1732. return err;
  1733. }
  1734. static void port_reset(struct mv643xx_eth_private *mp)
  1735. {
  1736. unsigned int data;
  1737. int i;
  1738. for (i = 0; i < 8; i++) {
  1739. if (mp->rxq_mask & (1 << i))
  1740. rxq_disable(mp->rxq + i);
  1741. if (mp->txq_mask & (1 << i))
  1742. txq_disable(mp->txq + i);
  1743. }
  1744. while (1) {
  1745. u32 ps = rdl(mp, PORT_STATUS(mp->port_num));
  1746. if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
  1747. break;
  1748. udelay(10);
  1749. }
  1750. /* Reset the Enable bit in the Configuration Register */
  1751. data = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
  1752. data &= ~(SERIAL_PORT_ENABLE |
  1753. DO_NOT_FORCE_LINK_FAIL |
  1754. FORCE_LINK_PASS);
  1755. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), data);
  1756. }
  1757. static int mv643xx_eth_stop(struct net_device *dev)
  1758. {
  1759. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1760. int i;
  1761. wrl(mp, INT_MASK(mp->port_num), 0x00000000);
  1762. rdl(mp, INT_MASK(mp->port_num));
  1763. napi_disable(&mp->napi);
  1764. del_timer_sync(&mp->rx_oom);
  1765. netif_carrier_off(dev);
  1766. netif_stop_queue(dev);
  1767. free_irq(dev->irq, dev);
  1768. port_reset(mp);
  1769. mib_counters_update(mp);
  1770. for (i = 0; i < 8; i++) {
  1771. if (mp->rxq_mask & (1 << i))
  1772. rxq_deinit(mp->rxq + i);
  1773. if (mp->txq_mask & (1 << i))
  1774. txq_deinit(mp->txq + i);
  1775. }
  1776. return 0;
  1777. }
  1778. static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1779. {
  1780. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1781. if (mp->phy_addr != -1)
  1782. return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
  1783. return -EOPNOTSUPP;
  1784. }
  1785. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  1786. {
  1787. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1788. if (new_mtu < 64 || new_mtu > 9500)
  1789. return -EINVAL;
  1790. dev->mtu = new_mtu;
  1791. tx_set_rate(mp, 1000000000, 16777216);
  1792. if (!netif_running(dev))
  1793. return 0;
  1794. /*
  1795. * Stop and then re-open the interface. This will allocate RX
  1796. * skbs of the new MTU.
  1797. * There is a possible danger that the open will not succeed,
  1798. * due to memory being full.
  1799. */
  1800. mv643xx_eth_stop(dev);
  1801. if (mv643xx_eth_open(dev)) {
  1802. dev_printk(KERN_ERR, &dev->dev,
  1803. "fatal error on re-opening device after "
  1804. "MTU change\n");
  1805. }
  1806. return 0;
  1807. }
  1808. static void tx_timeout_task(struct work_struct *ugly)
  1809. {
  1810. struct mv643xx_eth_private *mp;
  1811. mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
  1812. if (netif_running(mp->dev)) {
  1813. netif_stop_queue(mp->dev);
  1814. port_reset(mp);
  1815. port_start(mp);
  1816. __txq_maybe_wake(mp->txq + mp->txq_primary);
  1817. }
  1818. }
  1819. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  1820. {
  1821. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1822. dev_printk(KERN_INFO, &dev->dev, "tx timeout\n");
  1823. schedule_work(&mp->tx_timeout_task);
  1824. }
  1825. #ifdef CONFIG_NET_POLL_CONTROLLER
  1826. static void mv643xx_eth_netpoll(struct net_device *dev)
  1827. {
  1828. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1829. wrl(mp, INT_MASK(mp->port_num), 0x00000000);
  1830. rdl(mp, INT_MASK(mp->port_num));
  1831. mv643xx_eth_irq(dev->irq, dev);
  1832. wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
  1833. }
  1834. #endif
  1835. static int mv643xx_eth_mdio_read(struct net_device *dev, int addr, int reg)
  1836. {
  1837. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1838. return smi_reg_read(mp, addr, reg);
  1839. }
  1840. static void mv643xx_eth_mdio_write(struct net_device *dev, int addr, int reg, int val)
  1841. {
  1842. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1843. smi_reg_write(mp, addr, reg, val);
  1844. }
  1845. /* platform glue ************************************************************/
  1846. static void
  1847. mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
  1848. struct mbus_dram_target_info *dram)
  1849. {
  1850. void __iomem *base = msp->base;
  1851. u32 win_enable;
  1852. u32 win_protect;
  1853. int i;
  1854. for (i = 0; i < 6; i++) {
  1855. writel(0, base + WINDOW_BASE(i));
  1856. writel(0, base + WINDOW_SIZE(i));
  1857. if (i < 4)
  1858. writel(0, base + WINDOW_REMAP_HIGH(i));
  1859. }
  1860. win_enable = 0x3f;
  1861. win_protect = 0;
  1862. for (i = 0; i < dram->num_cs; i++) {
  1863. struct mbus_dram_window *cs = dram->cs + i;
  1864. writel((cs->base & 0xffff0000) |
  1865. (cs->mbus_attr << 8) |
  1866. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  1867. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  1868. win_enable &= ~(1 << i);
  1869. win_protect |= 3 << (2 * i);
  1870. }
  1871. writel(win_enable, base + WINDOW_BAR_ENABLE);
  1872. msp->win_protect = win_protect;
  1873. }
  1874. static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
  1875. {
  1876. /*
  1877. * Check whether we have a 14-bit coal limit field in bits
  1878. * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
  1879. * SDMA config register.
  1880. */
  1881. writel(0x02000000, msp->base + SDMA_CONFIG(0));
  1882. if (readl(msp->base + SDMA_CONFIG(0)) & 0x02000000)
  1883. msp->extended_rx_coal_limit = 1;
  1884. else
  1885. msp->extended_rx_coal_limit = 0;
  1886. /*
  1887. * Check whether the TX rate control registers are in the
  1888. * old or the new place.
  1889. */
  1890. writel(1, msp->base + TX_BW_MTU_MOVED(0));
  1891. if (readl(msp->base + TX_BW_MTU_MOVED(0)) & 1)
  1892. msp->tx_bw_control_moved = 1;
  1893. else
  1894. msp->tx_bw_control_moved = 0;
  1895. }
  1896. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  1897. {
  1898. static int mv643xx_eth_version_printed = 0;
  1899. struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
  1900. struct mv643xx_eth_shared_private *msp;
  1901. struct resource *res;
  1902. int ret;
  1903. if (!mv643xx_eth_version_printed++)
  1904. printk(KERN_NOTICE "MV-643xx 10/100/1000 ethernet "
  1905. "driver version %s\n", mv643xx_eth_driver_version);
  1906. ret = -EINVAL;
  1907. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1908. if (res == NULL)
  1909. goto out;
  1910. ret = -ENOMEM;
  1911. msp = kmalloc(sizeof(*msp), GFP_KERNEL);
  1912. if (msp == NULL)
  1913. goto out;
  1914. memset(msp, 0, sizeof(*msp));
  1915. msp->base = ioremap(res->start, res->end - res->start + 1);
  1916. if (msp->base == NULL)
  1917. goto out_free;
  1918. mutex_init(&msp->phy_lock);
  1919. msp->err_interrupt = NO_IRQ;
  1920. init_waitqueue_head(&msp->smi_busy_wait);
  1921. /*
  1922. * Check whether the error interrupt is hooked up.
  1923. */
  1924. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1925. if (res != NULL) {
  1926. int err;
  1927. err = request_irq(res->start, mv643xx_eth_err_irq,
  1928. IRQF_SHARED, "mv643xx_eth", msp);
  1929. if (!err) {
  1930. writel(ERR_INT_SMI_DONE, msp->base + ERR_INT_MASK);
  1931. msp->err_interrupt = res->start;
  1932. }
  1933. }
  1934. /*
  1935. * (Re-)program MBUS remapping windows if we are asked to.
  1936. */
  1937. if (pd != NULL && pd->dram != NULL)
  1938. mv643xx_eth_conf_mbus_windows(msp, pd->dram);
  1939. /*
  1940. * Detect hardware parameters.
  1941. */
  1942. msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
  1943. infer_hw_params(msp);
  1944. platform_set_drvdata(pdev, msp);
  1945. return 0;
  1946. out_free:
  1947. kfree(msp);
  1948. out:
  1949. return ret;
  1950. }
  1951. static int mv643xx_eth_shared_remove(struct platform_device *pdev)
  1952. {
  1953. struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
  1954. if (msp->err_interrupt != NO_IRQ)
  1955. free_irq(msp->err_interrupt, msp);
  1956. iounmap(msp->base);
  1957. kfree(msp);
  1958. return 0;
  1959. }
  1960. static struct platform_driver mv643xx_eth_shared_driver = {
  1961. .probe = mv643xx_eth_shared_probe,
  1962. .remove = mv643xx_eth_shared_remove,
  1963. .driver = {
  1964. .name = MV643XX_ETH_SHARED_NAME,
  1965. .owner = THIS_MODULE,
  1966. },
  1967. };
  1968. static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
  1969. {
  1970. int addr_shift = 5 * mp->port_num;
  1971. u32 data;
  1972. data = rdl(mp, PHY_ADDR);
  1973. data &= ~(0x1f << addr_shift);
  1974. data |= (phy_addr & 0x1f) << addr_shift;
  1975. wrl(mp, PHY_ADDR, data);
  1976. }
  1977. static int phy_addr_get(struct mv643xx_eth_private *mp)
  1978. {
  1979. unsigned int data;
  1980. data = rdl(mp, PHY_ADDR);
  1981. return (data >> (5 * mp->port_num)) & 0x1f;
  1982. }
  1983. static void set_params(struct mv643xx_eth_private *mp,
  1984. struct mv643xx_eth_platform_data *pd)
  1985. {
  1986. struct net_device *dev = mp->dev;
  1987. if (is_valid_ether_addr(pd->mac_addr))
  1988. memcpy(dev->dev_addr, pd->mac_addr, 6);
  1989. else
  1990. uc_addr_get(mp, dev->dev_addr);
  1991. if (pd->phy_addr == -1) {
  1992. mp->shared_smi = NULL;
  1993. mp->phy_addr = -1;
  1994. } else {
  1995. mp->shared_smi = mp->shared;
  1996. if (pd->shared_smi != NULL)
  1997. mp->shared_smi = platform_get_drvdata(pd->shared_smi);
  1998. if (pd->force_phy_addr || pd->phy_addr) {
  1999. mp->phy_addr = pd->phy_addr & 0x3f;
  2000. phy_addr_set(mp, mp->phy_addr);
  2001. } else {
  2002. mp->phy_addr = phy_addr_get(mp);
  2003. }
  2004. }
  2005. mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
  2006. if (pd->rx_queue_size)
  2007. mp->default_rx_ring_size = pd->rx_queue_size;
  2008. mp->rx_desc_sram_addr = pd->rx_sram_addr;
  2009. mp->rx_desc_sram_size = pd->rx_sram_size;
  2010. if (pd->rx_queue_mask)
  2011. mp->rxq_mask = pd->rx_queue_mask;
  2012. else
  2013. mp->rxq_mask = 0x01;
  2014. mp->rxq_primary = fls(mp->rxq_mask) - 1;
  2015. mp->default_tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
  2016. if (pd->tx_queue_size)
  2017. mp->default_tx_ring_size = pd->tx_queue_size;
  2018. mp->tx_desc_sram_addr = pd->tx_sram_addr;
  2019. mp->tx_desc_sram_size = pd->tx_sram_size;
  2020. if (pd->tx_queue_mask)
  2021. mp->txq_mask = pd->tx_queue_mask;
  2022. else
  2023. mp->txq_mask = 0x01;
  2024. mp->txq_primary = fls(mp->txq_mask) - 1;
  2025. }
  2026. static int phy_detect(struct mv643xx_eth_private *mp)
  2027. {
  2028. int data;
  2029. int data2;
  2030. data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
  2031. if (data < 0)
  2032. return -ENODEV;
  2033. if (smi_reg_write(mp, mp->phy_addr, MII_BMCR, data ^ BMCR_ANENABLE) < 0)
  2034. return -ENODEV;
  2035. data2 = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
  2036. if (data2 < 0)
  2037. return -ENODEV;
  2038. if (((data ^ data2) & BMCR_ANENABLE) == 0)
  2039. return -ENODEV;
  2040. smi_reg_write(mp, mp->phy_addr, MII_BMCR, data);
  2041. return 0;
  2042. }
  2043. static int phy_init(struct mv643xx_eth_private *mp,
  2044. struct mv643xx_eth_platform_data *pd)
  2045. {
  2046. struct ethtool_cmd cmd;
  2047. int err;
  2048. err = phy_detect(mp);
  2049. if (err) {
  2050. dev_printk(KERN_INFO, &mp->dev->dev,
  2051. "no PHY detected at addr %d\n", mp->phy_addr);
  2052. return err;
  2053. }
  2054. phy_reset(mp);
  2055. mp->mii.phy_id = mp->phy_addr;
  2056. mp->mii.phy_id_mask = 0x3f;
  2057. mp->mii.reg_num_mask = 0x1f;
  2058. mp->mii.dev = mp->dev;
  2059. mp->mii.mdio_read = mv643xx_eth_mdio_read;
  2060. mp->mii.mdio_write = mv643xx_eth_mdio_write;
  2061. mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
  2062. memset(&cmd, 0, sizeof(cmd));
  2063. cmd.port = PORT_MII;
  2064. cmd.transceiver = XCVR_INTERNAL;
  2065. cmd.phy_address = mp->phy_addr;
  2066. if (pd->speed == 0) {
  2067. cmd.autoneg = AUTONEG_ENABLE;
  2068. cmd.speed = SPEED_100;
  2069. cmd.advertising = ADVERTISED_10baseT_Half |
  2070. ADVERTISED_10baseT_Full |
  2071. ADVERTISED_100baseT_Half |
  2072. ADVERTISED_100baseT_Full;
  2073. if (mp->mii.supports_gmii)
  2074. cmd.advertising |= ADVERTISED_1000baseT_Full;
  2075. } else {
  2076. cmd.autoneg = AUTONEG_DISABLE;
  2077. cmd.speed = pd->speed;
  2078. cmd.duplex = pd->duplex;
  2079. }
  2080. mv643xx_eth_set_settings(mp->dev, &cmd);
  2081. return 0;
  2082. }
  2083. static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
  2084. {
  2085. u32 pscr;
  2086. pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
  2087. if (pscr & SERIAL_PORT_ENABLE) {
  2088. pscr &= ~SERIAL_PORT_ENABLE;
  2089. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  2090. }
  2091. pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
  2092. if (mp->phy_addr == -1) {
  2093. pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
  2094. if (speed == SPEED_1000)
  2095. pscr |= SET_GMII_SPEED_TO_1000;
  2096. else if (speed == SPEED_100)
  2097. pscr |= SET_MII_SPEED_TO_100;
  2098. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
  2099. pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
  2100. if (duplex == DUPLEX_FULL)
  2101. pscr |= SET_FULL_DUPLEX_MODE;
  2102. }
  2103. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  2104. }
  2105. static int mv643xx_eth_probe(struct platform_device *pdev)
  2106. {
  2107. struct mv643xx_eth_platform_data *pd;
  2108. struct mv643xx_eth_private *mp;
  2109. struct net_device *dev;
  2110. struct resource *res;
  2111. DECLARE_MAC_BUF(mac);
  2112. int err;
  2113. pd = pdev->dev.platform_data;
  2114. if (pd == NULL) {
  2115. dev_printk(KERN_ERR, &pdev->dev,
  2116. "no mv643xx_eth_platform_data\n");
  2117. return -ENODEV;
  2118. }
  2119. if (pd->shared == NULL) {
  2120. dev_printk(KERN_ERR, &pdev->dev,
  2121. "no mv643xx_eth_platform_data->shared\n");
  2122. return -ENODEV;
  2123. }
  2124. dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
  2125. if (!dev)
  2126. return -ENOMEM;
  2127. mp = netdev_priv(dev);
  2128. platform_set_drvdata(pdev, mp);
  2129. mp->shared = platform_get_drvdata(pd->shared);
  2130. mp->port_num = pd->port_number;
  2131. mp->dev = dev;
  2132. set_params(mp, pd);
  2133. spin_lock_init(&mp->lock);
  2134. mib_counters_clear(mp);
  2135. INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
  2136. if (mp->phy_addr != -1) {
  2137. err = phy_init(mp, pd);
  2138. if (err)
  2139. goto out;
  2140. SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
  2141. } else {
  2142. SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops_phyless);
  2143. }
  2144. init_pscr(mp, pd->speed, pd->duplex);
  2145. netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
  2146. init_timer(&mp->rx_oom);
  2147. mp->rx_oom.data = (unsigned long)mp;
  2148. mp->rx_oom.function = oom_timer_wrapper;
  2149. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2150. BUG_ON(!res);
  2151. dev->irq = res->start;
  2152. dev->hard_start_xmit = mv643xx_eth_xmit;
  2153. dev->open = mv643xx_eth_open;
  2154. dev->stop = mv643xx_eth_stop;
  2155. dev->set_multicast_list = mv643xx_eth_set_rx_mode;
  2156. dev->set_mac_address = mv643xx_eth_set_mac_address;
  2157. dev->do_ioctl = mv643xx_eth_ioctl;
  2158. dev->change_mtu = mv643xx_eth_change_mtu;
  2159. dev->tx_timeout = mv643xx_eth_tx_timeout;
  2160. #ifdef CONFIG_NET_POLL_CONTROLLER
  2161. dev->poll_controller = mv643xx_eth_netpoll;
  2162. #endif
  2163. dev->watchdog_timeo = 2 * HZ;
  2164. dev->base_addr = 0;
  2165. #ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
  2166. /*
  2167. * Zero copy can only work if we use Discovery II memory. Else, we will
  2168. * have to map the buffers to ISA memory which is only 16 MB
  2169. */
  2170. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
  2171. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
  2172. #endif
  2173. SET_NETDEV_DEV(dev, &pdev->dev);
  2174. if (mp->shared->win_protect)
  2175. wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
  2176. err = register_netdev(dev);
  2177. if (err)
  2178. goto out;
  2179. dev_printk(KERN_NOTICE, &dev->dev, "port %d with MAC address %s\n",
  2180. mp->port_num, print_mac(mac, dev->dev_addr));
  2181. if (dev->features & NETIF_F_SG)
  2182. dev_printk(KERN_NOTICE, &dev->dev, "scatter/gather enabled\n");
  2183. if (dev->features & NETIF_F_IP_CSUM)
  2184. dev_printk(KERN_NOTICE, &dev->dev, "tx checksum offload\n");
  2185. if (mp->tx_desc_sram_size > 0)
  2186. dev_printk(KERN_NOTICE, &dev->dev, "configured with sram\n");
  2187. return 0;
  2188. out:
  2189. free_netdev(dev);
  2190. return err;
  2191. }
  2192. static int mv643xx_eth_remove(struct platform_device *pdev)
  2193. {
  2194. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2195. unregister_netdev(mp->dev);
  2196. flush_scheduled_work();
  2197. free_netdev(mp->dev);
  2198. platform_set_drvdata(pdev, NULL);
  2199. return 0;
  2200. }
  2201. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  2202. {
  2203. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2204. /* Mask all interrupts on ethernet port */
  2205. wrl(mp, INT_MASK(mp->port_num), 0);
  2206. rdl(mp, INT_MASK(mp->port_num));
  2207. if (netif_running(mp->dev))
  2208. port_reset(mp);
  2209. }
  2210. static struct platform_driver mv643xx_eth_driver = {
  2211. .probe = mv643xx_eth_probe,
  2212. .remove = mv643xx_eth_remove,
  2213. .shutdown = mv643xx_eth_shutdown,
  2214. .driver = {
  2215. .name = MV643XX_ETH_NAME,
  2216. .owner = THIS_MODULE,
  2217. },
  2218. };
  2219. static int __init mv643xx_eth_init_module(void)
  2220. {
  2221. int rc;
  2222. rc = platform_driver_register(&mv643xx_eth_shared_driver);
  2223. if (!rc) {
  2224. rc = platform_driver_register(&mv643xx_eth_driver);
  2225. if (rc)
  2226. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2227. }
  2228. return rc;
  2229. }
  2230. module_init(mv643xx_eth_init_module);
  2231. static void __exit mv643xx_eth_cleanup_module(void)
  2232. {
  2233. platform_driver_unregister(&mv643xx_eth_driver);
  2234. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2235. }
  2236. module_exit(mv643xx_eth_cleanup_module);
  2237. MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
  2238. "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
  2239. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  2240. MODULE_LICENSE("GPL");
  2241. MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
  2242. MODULE_ALIAS("platform:" MV643XX_ETH_NAME);