mv_eth.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344
  1. /*
  2. * (C) Copyright 2003
  3. * Ingo Assmus <ingo.assmus@keymile.com>
  4. *
  5. * based on - Driver for MV64460X ethernet ports
  6. * Copyright (C) 2002 rabeeh@galileo.co.il
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. 3 the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * mv_eth.c - header file for the polled mode GT ethernet driver
  28. */
  29. #include <common.h>
  30. #include <net.h>
  31. #include <malloc.h>
  32. #include <miiphy.h>
  33. #include "mv_eth.h"
  34. /* enable Debug outputs */
  35. #undef DEBUG_MV_ETH
  36. #ifdef DEBUG_MV_ETH
  37. #define DEBUG
  38. #define DP(x) x
  39. #else
  40. #define DP(x)
  41. #endif
  42. /* PHY DFCDL Registers */
  43. #define ETH_PHY_DFCDL_CONFIG0_REG 0x2100
  44. #define ETH_PHY_DFCDL_CONFIG1_REG 0x2104
  45. #define ETH_PHY_DFCDL_ADDR_REG 0x2110
  46. #define ETH_PHY_DFCDL_DATA0_REG 0x2114
  47. #define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
  48. #define PHY_UPDATE_TIMEOUT 10000
  49. #undef MV64460_CHECKSUM_OFFLOAD
  50. /*************************************************************************
  51. * The first part is the high level driver of the gigE ethernet ports. *
  52. *************************************************************************/
  53. /* Definition for configuring driver */
  54. /* #define UPDATE_STATS_BY_SOFTWARE */
  55. #undef MV64460_RX_QUEUE_FILL_ON_TASK
  56. /* Constants */
  57. #define MAGIC_ETH_RUNNING 8031971
  58. #define MV64460_INTERNAL_SRAM_SIZE _256K
  59. #define EXTRA_BYTES 32
  60. #define WRAP ETH_HLEN + 2 + 4 + 16
  61. #define BUFFER_MTU dev->mtu + WRAP
  62. #define INT_CAUSE_UNMASK_ALL 0x0007ffff
  63. #define INT_CAUSE_UNMASK_ALL_EXT 0x0011ffff
  64. #ifdef MV64460_RX_FILL_ON_TASK
  65. #define INT_CAUSE_MASK_ALL 0x00000000
  66. #define INT_CAUSE_CHECK_BITS INT_CAUSE_UNMASK_ALL
  67. #define INT_CAUSE_CHECK_BITS_EXT INT_CAUSE_UNMASK_ALL_EXT
  68. #endif
  69. /* Read/Write to/from MV64460 internal registers */
  70. #define MV_REG_READ(offset) my_le32_to_cpu(* (volatile unsigned int *) (INTERNAL_REG_BASE_ADDR + offset))
  71. #define MV_REG_WRITE(offset,data) *(volatile unsigned int *) (INTERNAL_REG_BASE_ADDR + offset) = my_cpu_to_le32 (data)
  72. #define MV_SET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) |= ((unsigned int)my_cpu_to_le32(bits)))
  73. #define MV_RESET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) &= ~((unsigned int)my_cpu_to_le32(bits)))
  74. #define my_cpu_to_le32(x) my_le32_to_cpu((x))
  75. /* Static function declarations */
  76. static int mv64460_eth_real_open (struct eth_device *eth);
  77. static int mv64460_eth_real_stop (struct eth_device *eth);
  78. static struct net_device_stats *mv64460_eth_get_stats (struct eth_device
  79. *dev);
  80. static void eth_port_init_mac_tables (ETH_PORT eth_port_num);
  81. static void mv64460_eth_update_stat (struct eth_device *dev);
  82. bool db64460_eth_start (struct eth_device *eth);
  83. unsigned int eth_read_mib_counter (ETH_PORT eth_port_num,
  84. unsigned int mib_offset);
  85. int mv64460_eth_receive (struct eth_device *dev);
  86. int mv64460_eth_xmit (struct eth_device *, volatile void *packet, int length);
  87. int mv_miiphy_read(char *devname, unsigned char phy_addr,
  88. unsigned char phy_reg, unsigned short *value);
  89. int mv_miiphy_write(char *devname, unsigned char phy_addr,
  90. unsigned char phy_reg, unsigned short value);
  91. int phy_setup_aneg (char *devname, unsigned char addr);
  92. #ifndef UPDATE_STATS_BY_SOFTWARE
  93. static void mv64460_eth_print_stat (struct eth_device *dev);
  94. #endif
  95. /* Processes a received packet */
  96. extern void NetReceive (volatile uchar *, int);
  97. extern unsigned int INTERNAL_REG_BASE_ADDR;
  98. unsigned long my_le32_to_cpu (unsigned long x)
  99. {
  100. return (((x & 0x000000ffU) << 24) |
  101. ((x & 0x0000ff00U) << 8) |
  102. ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
  103. }
  104. /*************************************************
  105. *Helper functions - used inside the driver only *
  106. *************************************************/
  107. #ifdef DEBUG_MV_ETH
  108. void print_globals (struct eth_device *dev)
  109. {
  110. printf ("Ethernet PRINT_Globals-Debug function\n");
  111. printf ("Base Address for ETH_PORT_INFO: %08x\n",
  112. (unsigned int) dev->priv);
  113. printf ("Base Address for mv64460_eth_priv: %08x\n",
  114. (unsigned int) &(((ETH_PORT_INFO *) dev->priv)->
  115. port_private));
  116. printf ("GT Internal Base Address: %08x\n",
  117. INTERNAL_REG_BASE_ADDR);
  118. printf ("Base Address for TX-DESCs: %08x Number of allocated Buffers %d\n",
  119. (unsigned int) ((ETH_PORT_INFO *) dev->priv)->p_tx_desc_area_base[0], MV64460_TX_QUEUE_SIZE);
  120. printf ("Base Address for RX-DESCs: %08x Number of allocated Buffers %d\n",
  121. (unsigned int) ((ETH_PORT_INFO *) dev->priv)->p_rx_desc_area_base[0], MV64460_RX_QUEUE_SIZE);
  122. printf ("Base Address for RX-Buffer: %08x allocated Bytes %d\n",
  123. (unsigned int) ((ETH_PORT_INFO *) dev->priv)->
  124. p_rx_buffer_base[0],
  125. (MV64460_RX_QUEUE_SIZE * MV64460_RX_BUFFER_SIZE) + 32);
  126. printf ("Base Address for TX-Buffer: %08x allocated Bytes %d\n",
  127. (unsigned int) ((ETH_PORT_INFO *) dev->priv)->
  128. p_tx_buffer_base[0],
  129. (MV64460_TX_QUEUE_SIZE * MV64460_TX_BUFFER_SIZE) + 32);
  130. }
  131. #endif
  132. /**********************************************************************
  133. * mv64460_eth_print_phy_status
  134. *
  135. * Prints gigabit ethenret phy status
  136. *
  137. * Input : pointer to ethernet interface network device structure
  138. * Output : N/A
  139. **********************************************************************/
  140. void mv64460_eth_print_phy_status (struct eth_device *dev)
  141. {
  142. struct mv64460_eth_priv *port_private;
  143. unsigned int port_num;
  144. ETH_PORT_INFO *ethernet_private = (ETH_PORT_INFO *) dev->priv;
  145. unsigned int port_status, phy_reg_data;
  146. port_private =
  147. (struct mv64460_eth_priv *) ethernet_private->port_private;
  148. port_num = port_private->port_num;
  149. /* Check Link status on phy */
  150. eth_port_read_smi_reg (port_num, 1, &phy_reg_data);
  151. if (!(phy_reg_data & 0x20)) {
  152. printf ("Ethernet port changed link status to DOWN\n");
  153. } else {
  154. port_status =
  155. MV_REG_READ (MV64460_ETH_PORT_STATUS_REG (port_num));
  156. printf ("Ethernet status port %d: Link up", port_num);
  157. printf (", %s",
  158. (port_status & BIT2) ? "Full Duplex" : "Half Duplex");
  159. if (port_status & BIT4)
  160. printf (", Speed 1 Gbps");
  161. else
  162. printf (", %s",
  163. (port_status & BIT5) ? "Speed 100 Mbps" :
  164. "Speed 10 Mbps");
  165. printf ("\n");
  166. }
  167. }
  168. /**********************************************************************
  169. * u-boot entry functions for mv64460_eth
  170. *
  171. **********************************************************************/
  172. int db64460_eth_probe (struct eth_device *dev)
  173. {
  174. return ((int) db64460_eth_start (dev));
  175. }
  176. int db64460_eth_poll (struct eth_device *dev)
  177. {
  178. return mv64460_eth_receive (dev);
  179. }
  180. int db64460_eth_transmit (struct eth_device *dev, volatile void *packet,
  181. int length)
  182. {
  183. mv64460_eth_xmit (dev, packet, length);
  184. return 0;
  185. }
  186. void db64460_eth_disable (struct eth_device *dev)
  187. {
  188. mv64460_eth_stop (dev);
  189. }
  190. #define DFCDL(write,read) ((write << 6) | read)
  191. unsigned int ethDfcdls[] = {
  192. DFCDL(0,0), DFCDL(1,1), DFCDL(2,2), DFCDL(3,3),
  193. DFCDL(4,4), DFCDL(5,5), DFCDL(6,6), DFCDL(7,7),
  194. DFCDL(8,8), DFCDL(9,9), DFCDL(10,10), DFCDL(11,11),
  195. DFCDL(12,12), DFCDL(13,13), DFCDL(14,14), DFCDL(15,15),
  196. DFCDL(16,16), DFCDL(17,17), DFCDL(18,18), DFCDL(19,19),
  197. DFCDL(20,20), DFCDL(21,21), DFCDL(22,22), DFCDL(23,23),
  198. DFCDL(24,24), DFCDL(25,25), DFCDL(26,26), DFCDL(27,27),
  199. DFCDL(28,28), DFCDL(29,29), DFCDL(30,30), DFCDL(31,31),
  200. DFCDL(32,32), DFCDL(33,33), DFCDL(34,34), DFCDL(35,35),
  201. DFCDL(36,36), DFCDL(37,37), DFCDL(38,38), DFCDL(39,39),
  202. DFCDL(40,40), DFCDL(41,41), DFCDL(42,42), DFCDL(43,43),
  203. DFCDL(44,44), DFCDL(45,45), DFCDL(46,46), DFCDL(47,47),
  204. DFCDL(48,48), DFCDL(49,49), DFCDL(50,50), DFCDL(51,51),
  205. DFCDL(52,52), DFCDL(53,53), DFCDL(54,54), DFCDL(55,55),
  206. DFCDL(56,56), DFCDL(57,57), DFCDL(58,58), DFCDL(59,59),
  207. DFCDL(60,60), DFCDL(61,61), DFCDL(62,62), DFCDL(63,63),
  208. };
  209. void mv_eth_phy_init (void)
  210. {
  211. int i;
  212. MV_REG_WRITE (ETH_PHY_DFCDL_ADDR_REG, 0);
  213. for (i = 0; i < 64; i++) {
  214. MV_REG_WRITE (ETH_PHY_DFCDL_DATA0_REG, ethDfcdls[i]);
  215. }
  216. MV_REG_WRITE (ETH_PHY_DFCDL_CONFIG0_REG, 0x300000);
  217. }
  218. void mv6446x_eth_initialize (bd_t * bis)
  219. {
  220. struct eth_device *dev;
  221. ETH_PORT_INFO *ethernet_private;
  222. struct mv64460_eth_priv *port_private;
  223. int devnum, x, temp;
  224. char *s, *e, buf[64];
  225. /* P3M750 only
  226. * Set RGMII clock drives strength
  227. */
  228. temp = MV_REG_READ(0x20A0);
  229. temp |= 0x04000080;
  230. MV_REG_WRITE(0x20A0, temp);
  231. mv_eth_phy_init();
  232. for (devnum = 0; devnum < MV_ETH_DEVS; devnum++) {
  233. dev = calloc (sizeof (*dev), 1);
  234. if (!dev) {
  235. printf ("%s: mv_enet%d allocation failure, %s\n",
  236. __FUNCTION__, devnum, "eth_device structure");
  237. return;
  238. }
  239. /* must be less than NAMESIZE (16) */
  240. sprintf (dev->name, "mv_enet%d", devnum);
  241. #ifdef DEBUG
  242. printf ("Initializing %s\n", dev->name);
  243. #endif
  244. /* Extract the MAC address from the environment */
  245. switch (devnum) {
  246. case 0:
  247. s = "ethaddr";
  248. break;
  249. case 1:
  250. s = "eth1addr";
  251. break;
  252. case 2:
  253. s = "eth2addr";
  254. break;
  255. default: /* this should never happen */
  256. printf ("%s: Invalid device number %d\n",
  257. __FUNCTION__, devnum);
  258. return;
  259. }
  260. temp = getenv_r (s, buf, sizeof (buf));
  261. s = (temp > 0) ? buf : NULL;
  262. #ifdef DEBUG
  263. printf ("Setting MAC %d to %s\n", devnum, s);
  264. #endif
  265. for (x = 0; x < 6; ++x) {
  266. dev->enetaddr[x] = s ? simple_strtoul (s, &e, 16) : 0;
  267. if (s)
  268. s = (*e) ? e + 1 : e;
  269. }
  270. /* ronen - set the MAC addr in the HW */
  271. eth_port_uc_addr_set (devnum, dev->enetaddr, 0);
  272. dev->init = (void *) db64460_eth_probe;
  273. dev->halt = (void *) ethernet_phy_reset;
  274. dev->send = (void *) db64460_eth_transmit;
  275. dev->recv = (void *) db64460_eth_poll;
  276. ethernet_private = calloc (sizeof (*ethernet_private), 1);
  277. dev->priv = (void *)ethernet_private;
  278. if (!ethernet_private) {
  279. printf ("%s: %s allocation failure, %s\n",
  280. __FUNCTION__, dev->name,
  281. "Private Device Structure");
  282. free (dev);
  283. return;
  284. }
  285. /* start with an zeroed ETH_PORT_INFO */
  286. memset (ethernet_private, 0, sizeof (ETH_PORT_INFO));
  287. memcpy (ethernet_private->port_mac_addr, dev->enetaddr, 6);
  288. /* set pointer to memory for stats data structure etc... */
  289. port_private = calloc (sizeof (*ethernet_private), 1);
  290. ethernet_private->port_private = (void *)port_private;
  291. if (!port_private) {
  292. printf ("%s: %s allocation failure, %s\n",
  293. __FUNCTION__, dev->name,
  294. "Port Private Device Structure");
  295. free (ethernet_private);
  296. free (dev);
  297. return;
  298. }
  299. port_private->stats =
  300. calloc (sizeof (struct net_device_stats), 1);
  301. if (!port_private->stats) {
  302. printf ("%s: %s allocation failure, %s\n",
  303. __FUNCTION__, dev->name,
  304. "Net stat Structure");
  305. free (port_private);
  306. free (ethernet_private);
  307. free (dev);
  308. return;
  309. }
  310. memset (ethernet_private->port_private, 0,
  311. sizeof (struct mv64460_eth_priv));
  312. switch (devnum) {
  313. case 0:
  314. ethernet_private->port_num = ETH_0;
  315. break;
  316. case 1:
  317. ethernet_private->port_num = ETH_1;
  318. break;
  319. case 2:
  320. ethernet_private->port_num = ETH_2;
  321. break;
  322. default:
  323. printf ("Invalid device number %d\n", devnum);
  324. break;
  325. };
  326. port_private->port_num = devnum;
  327. /*
  328. * Read MIB counter on the GT in order to reset them,
  329. * then zero all the stats fields in memory
  330. */
  331. mv64460_eth_update_stat (dev);
  332. memset (port_private->stats, 0,
  333. sizeof (struct net_device_stats));
  334. /* Extract the MAC address from the environment */
  335. switch (devnum) {
  336. case 0:
  337. s = "ethaddr";
  338. break;
  339. case 1:
  340. s = "eth1addr";
  341. break;
  342. case 2:
  343. s = "eth2addr";
  344. break;
  345. default: /* this should never happen */
  346. printf ("%s: Invalid device number %d\n",
  347. __FUNCTION__, devnum);
  348. return;
  349. }
  350. temp = getenv_r (s, buf, sizeof (buf));
  351. s = (temp > 0) ? buf : NULL;
  352. #ifdef DEBUG
  353. printf ("Setting MAC %d to %s\n", devnum, s);
  354. #endif
  355. for (x = 0; x < 6; ++x) {
  356. dev->enetaddr[x] = s ? simple_strtoul (s, &e, 16) : 0;
  357. if (s)
  358. s = (*e) ? e + 1 : e;
  359. }
  360. DP (printf ("Allocating descriptor and buffer rings\n"));
  361. ethernet_private->p_rx_desc_area_base[0] =
  362. (ETH_RX_DESC *) memalign (16,
  363. RX_DESC_ALIGNED_SIZE *
  364. MV64460_RX_QUEUE_SIZE + 1);
  365. ethernet_private->p_tx_desc_area_base[0] =
  366. (ETH_TX_DESC *) memalign (16,
  367. TX_DESC_ALIGNED_SIZE *
  368. MV64460_TX_QUEUE_SIZE + 1);
  369. ethernet_private->p_rx_buffer_base[0] =
  370. (char *) memalign (16,
  371. MV64460_RX_QUEUE_SIZE *
  372. MV64460_TX_BUFFER_SIZE + 1);
  373. ethernet_private->p_tx_buffer_base[0] =
  374. (char *) memalign (16,
  375. MV64460_RX_QUEUE_SIZE *
  376. MV64460_TX_BUFFER_SIZE + 1);
  377. #ifdef DEBUG_MV_ETH
  378. /* DEBUG OUTPUT prints adresses of globals */
  379. print_globals (dev);
  380. #endif
  381. eth_register (dev);
  382. miiphy_register(dev->name, mv_miiphy_read, mv_miiphy_write);
  383. }
  384. DP (printf ("%s: exit\n", __FUNCTION__));
  385. }
  386. /**********************************************************************
  387. * mv64460_eth_open
  388. *
  389. * This function is called when openning the network device. The function
  390. * should initialize all the hardware, initialize cyclic Rx/Tx
  391. * descriptors chain and buffers and allocate an IRQ to the network
  392. * device.
  393. *
  394. * Input : a pointer to the network device structure
  395. * / / ronen - changed the output to match net/eth.c needs
  396. * Output : nonzero of success , zero if fails.
  397. * under construction
  398. **********************************************************************/
  399. int mv64460_eth_open (struct eth_device *dev)
  400. {
  401. return (mv64460_eth_real_open (dev));
  402. }
  403. /* Helper function for mv64460_eth_open */
  404. static int mv64460_eth_real_open (struct eth_device *dev)
  405. {
  406. unsigned int queue;
  407. ETH_PORT_INFO *ethernet_private;
  408. struct mv64460_eth_priv *port_private;
  409. unsigned int port_num;
  410. u32 port_status;
  411. ushort reg_short;
  412. int speed;
  413. int duplex;
  414. int i;
  415. int reg;
  416. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  417. /* ronen - when we update the MAC env params we only update dev->enetaddr
  418. see ./net/eth.c eth_set_enetaddr() */
  419. memcpy (ethernet_private->port_mac_addr, dev->enetaddr, 6);
  420. port_private = (struct mv64460_eth_priv *) ethernet_private->port_private;
  421. port_num = port_private->port_num;
  422. /* Stop RX Queues */
  423. MV_REG_WRITE (MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG (port_num), 0x0000ff00);
  424. /* Clear the ethernet port interrupts */
  425. MV_REG_WRITE (MV64460_ETH_INTERRUPT_CAUSE_REG (port_num), 0);
  426. MV_REG_WRITE (MV64460_ETH_INTERRUPT_CAUSE_EXTEND_REG (port_num), 0);
  427. /* Unmask RX buffer and TX end interrupt */
  428. MV_REG_WRITE (MV64460_ETH_INTERRUPT_MASK_REG (port_num),
  429. INT_CAUSE_UNMASK_ALL);
  430. /* Unmask phy and link status changes interrupts */
  431. MV_REG_WRITE (MV64460_ETH_INTERRUPT_EXTEND_MASK_REG (port_num),
  432. INT_CAUSE_UNMASK_ALL_EXT);
  433. /* Set phy address of the port */
  434. ethernet_private->port_phy_addr = 0x1 + (port_num << 1);
  435. reg = ethernet_private->port_phy_addr;
  436. /* Activate the DMA channels etc */
  437. eth_port_init (ethernet_private);
  438. /* "Allocate" setup TX rings */
  439. for (queue = 0; queue < MV64460_TX_QUEUE_NUM; queue++) {
  440. unsigned int size;
  441. port_private->tx_ring_size[queue] = MV64460_TX_QUEUE_SIZE;
  442. size = (port_private->tx_ring_size[queue] * TX_DESC_ALIGNED_SIZE); /*size = no of DESCs times DESC-size */
  443. ethernet_private->tx_desc_area_size[queue] = size;
  444. /* first clear desc area completely */
  445. memset ((void *) ethernet_private->p_tx_desc_area_base[queue],
  446. 0, ethernet_private->tx_desc_area_size[queue]);
  447. /* initialize tx desc ring with low level driver */
  448. if (ether_init_tx_desc_ring
  449. (ethernet_private, ETH_Q0,
  450. port_private->tx_ring_size[queue],
  451. MV64460_TX_BUFFER_SIZE /* Each Buffer is 1600 Byte */ ,
  452. (unsigned int) ethernet_private->
  453. p_tx_desc_area_base[queue],
  454. (unsigned int) ethernet_private->
  455. p_tx_buffer_base[queue]) == false)
  456. printf ("### Error initializing TX Ring\n");
  457. }
  458. /* "Allocate" setup RX rings */
  459. for (queue = 0; queue < MV64460_RX_QUEUE_NUM; queue++) {
  460. unsigned int size;
  461. /* Meantime RX Ring are fixed - but must be configurable by user */
  462. port_private->rx_ring_size[queue] = MV64460_RX_QUEUE_SIZE;
  463. size = (port_private->rx_ring_size[queue] *
  464. RX_DESC_ALIGNED_SIZE);
  465. ethernet_private->rx_desc_area_size[queue] = size;
  466. /* first clear desc area completely */
  467. memset ((void *) ethernet_private->p_rx_desc_area_base[queue],
  468. 0, ethernet_private->rx_desc_area_size[queue]);
  469. if ((ether_init_rx_desc_ring
  470. (ethernet_private, ETH_Q0,
  471. port_private->rx_ring_size[queue],
  472. MV64460_RX_BUFFER_SIZE /* Each Buffer is 1600 Byte */ ,
  473. (unsigned int) ethernet_private->
  474. p_rx_desc_area_base[queue],
  475. (unsigned int) ethernet_private->
  476. p_rx_buffer_base[queue])) == false)
  477. printf ("### Error initializing RX Ring\n");
  478. }
  479. eth_port_start (ethernet_private);
  480. /* Set maximum receive buffer to 9700 bytes */
  481. MV_REG_WRITE (MV64460_ETH_PORT_SERIAL_CONTROL_REG (port_num),
  482. (0x5 << 17) |
  483. (MV_REG_READ
  484. (MV64460_ETH_PORT_SERIAL_CONTROL_REG (port_num))
  485. & 0xfff1ffff));
  486. /*
  487. * Set ethernet MTU for leaky bucket mechanism to 0 - this will
  488. * disable the leaky bucket mechanism .
  489. */
  490. MV_REG_WRITE (MV64460_ETH_MAXIMUM_TRANSMIT_UNIT (port_num), 0);
  491. port_status = MV_REG_READ (MV64460_ETH_PORT_STATUS_REG (port_num));
  492. #if defined(CONFIG_PHY_RESET)
  493. /*
  494. * Reset the phy, only if its the first time through
  495. * otherwise, just check the speeds & feeds
  496. */
  497. if (port_private->first_init == 0) {
  498. port_private->first_init = 1;
  499. ethernet_phy_reset (port_num);
  500. /* Start/Restart autonegotiation */
  501. phy_setup_aneg (dev->name, reg);
  502. udelay (1000);
  503. }
  504. #endif /* defined(CONFIG_PHY_RESET) */
  505. miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
  506. /*
  507. * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
  508. */
  509. if ((reg_short & PHY_BMSR_AUTN_ABLE)
  510. && !(reg_short & PHY_BMSR_AUTN_COMP)) {
  511. puts ("Waiting for PHY auto negotiation to complete");
  512. i = 0;
  513. while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
  514. /*
  515. * Timeout reached ?
  516. */
  517. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  518. puts (" TIMEOUT !\n");
  519. break;
  520. }
  521. if ((i++ % 1000) == 0) {
  522. putc ('.');
  523. }
  524. udelay (1000); /* 1 ms */
  525. miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
  526. }
  527. puts (" done\n");
  528. udelay (500000); /* another 500 ms (results in faster booting) */
  529. }
  530. speed = miiphy_speed (dev->name, reg);
  531. duplex = miiphy_duplex (dev->name, reg);
  532. printf ("ENET Speed is %d Mbps - %s duplex connection\n",
  533. (int) speed, (duplex == HALF) ? "HALF" : "FULL");
  534. port_private->eth_running = MAGIC_ETH_RUNNING;
  535. return 1;
  536. }
  537. static int mv64460_eth_free_tx_rings (struct eth_device *dev)
  538. {
  539. unsigned int queue;
  540. ETH_PORT_INFO *ethernet_private;
  541. struct mv64460_eth_priv *port_private;
  542. unsigned int port_num;
  543. volatile ETH_TX_DESC *p_tx_curr_desc;
  544. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  545. port_private =
  546. (struct mv64460_eth_priv *) ethernet_private->port_private;
  547. port_num = port_private->port_num;
  548. /* Stop Tx Queues */
  549. MV_REG_WRITE (MV64460_ETH_TRANSMIT_QUEUE_COMMAND_REG (port_num),
  550. 0x0000ff00);
  551. /* Free TX rings */
  552. DP (printf ("Clearing previously allocated TX queues... "));
  553. for (queue = 0; queue < MV64460_TX_QUEUE_NUM; queue++) {
  554. /* Free on TX rings */
  555. for (p_tx_curr_desc =
  556. ethernet_private->p_tx_desc_area_base[queue];
  557. ((unsigned int) p_tx_curr_desc <= (unsigned int)
  558. ethernet_private->p_tx_desc_area_base[queue] +
  559. ethernet_private->tx_desc_area_size[queue]);
  560. p_tx_curr_desc =
  561. (ETH_TX_DESC *) ((unsigned int) p_tx_curr_desc +
  562. TX_DESC_ALIGNED_SIZE)) {
  563. /* this is inside for loop */
  564. if (p_tx_curr_desc->return_info != 0) {
  565. p_tx_curr_desc->return_info = 0;
  566. DP (printf ("freed\n"));
  567. }
  568. }
  569. DP (printf ("Done\n"));
  570. }
  571. return 0;
  572. }
  573. static int mv64460_eth_free_rx_rings (struct eth_device *dev)
  574. {
  575. unsigned int queue;
  576. ETH_PORT_INFO *ethernet_private;
  577. struct mv64460_eth_priv *port_private;
  578. unsigned int port_num;
  579. volatile ETH_RX_DESC *p_rx_curr_desc;
  580. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  581. port_private =
  582. (struct mv64460_eth_priv *) ethernet_private->port_private;
  583. port_num = port_private->port_num;
  584. /* Stop RX Queues */
  585. MV_REG_WRITE (MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG (port_num),
  586. 0x0000ff00);
  587. /* Free RX rings */
  588. DP (printf ("Clearing previously allocated RX queues... "));
  589. for (queue = 0; queue < MV64460_RX_QUEUE_NUM; queue++) {
  590. /* Free preallocated skb's on RX rings */
  591. for (p_rx_curr_desc =
  592. ethernet_private->p_rx_desc_area_base[queue];
  593. (((unsigned int) p_rx_curr_desc <
  594. ((unsigned int) ethernet_private->
  595. p_rx_desc_area_base[queue] +
  596. ethernet_private->rx_desc_area_size[queue])));
  597. p_rx_curr_desc =
  598. (ETH_RX_DESC *) ((unsigned int) p_rx_curr_desc +
  599. RX_DESC_ALIGNED_SIZE)) {
  600. if (p_rx_curr_desc->return_info != 0) {
  601. p_rx_curr_desc->return_info = 0;
  602. DP (printf ("freed\n"));
  603. }
  604. }
  605. DP (printf ("Done\n"));
  606. }
  607. return 0;
  608. }
  609. /**********************************************************************
  610. * mv64460_eth_stop
  611. *
  612. * This function is used when closing the network device.
  613. * It updates the hardware,
  614. * release all memory that holds buffers and descriptors and release the IRQ.
  615. * Input : a pointer to the device structure
  616. * Output : zero if success , nonzero if fails
  617. *********************************************************************/
  618. int mv64460_eth_stop (struct eth_device *dev)
  619. {
  620. ETH_PORT_INFO *ethernet_private;
  621. struct mv64460_eth_priv *port_private;
  622. unsigned int port_num;
  623. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  624. port_private =
  625. (struct mv64460_eth_priv *) ethernet_private->port_private;
  626. port_num = port_private->port_num;
  627. /* Disable all gigE address decoder */
  628. MV_REG_WRITE (MV64460_ETH_BASE_ADDR_ENABLE_REG, 0x3f);
  629. DP (printf ("%s Ethernet stop called ... \n", __FUNCTION__));
  630. mv64460_eth_real_stop (dev);
  631. return 0;
  632. };
  633. /* Helper function for mv64460_eth_stop */
  634. static int mv64460_eth_real_stop (struct eth_device *dev)
  635. {
  636. ETH_PORT_INFO *ethernet_private;
  637. struct mv64460_eth_priv *port_private;
  638. unsigned int port_num;
  639. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  640. port_private =
  641. (struct mv64460_eth_priv *) ethernet_private->port_private;
  642. port_num = port_private->port_num;
  643. mv64460_eth_free_tx_rings (dev);
  644. mv64460_eth_free_rx_rings (dev);
  645. eth_port_reset (ethernet_private->port_num);
  646. /* Disable ethernet port interrupts */
  647. MV_REG_WRITE (MV64460_ETH_INTERRUPT_CAUSE_REG (port_num), 0);
  648. MV_REG_WRITE (MV64460_ETH_INTERRUPT_CAUSE_EXTEND_REG (port_num), 0);
  649. /* Mask RX buffer and TX end interrupt */
  650. MV_REG_WRITE (MV64460_ETH_INTERRUPT_MASK_REG (port_num), 0);
  651. /* Mask phy and link status changes interrupts */
  652. MV_REG_WRITE (MV64460_ETH_INTERRUPT_EXTEND_MASK_REG (port_num), 0);
  653. MV_RESET_REG_BITS (MV64460_CPU_INTERRUPT0_MASK_HIGH,
  654. BIT0 << port_num);
  655. /* Print Network statistics */
  656. #ifndef UPDATE_STATS_BY_SOFTWARE
  657. /*
  658. * Print statistics (only if ethernet is running),
  659. * then zero all the stats fields in memory
  660. */
  661. if (port_private->eth_running == MAGIC_ETH_RUNNING) {
  662. port_private->eth_running = 0;
  663. mv64460_eth_print_stat (dev);
  664. }
  665. memset (port_private->stats, 0, sizeof (struct net_device_stats));
  666. #endif
  667. DP (printf ("\nEthernet stopped ... \n"));
  668. return 0;
  669. }
  670. /**********************************************************************
  671. * mv64460_eth_start_xmit
  672. *
  673. * This function is queues a packet in the Tx descriptor for
  674. * required port.
  675. *
  676. * Input : skb - a pointer to socket buffer
  677. * dev - a pointer to the required port
  678. *
  679. * Output : zero upon success
  680. **********************************************************************/
  681. int mv64460_eth_xmit (struct eth_device *dev, volatile void *dataPtr,
  682. int dataSize)
  683. {
  684. ETH_PORT_INFO *ethernet_private;
  685. struct mv64460_eth_priv *port_private;
  686. unsigned int port_num;
  687. PKT_INFO pkt_info;
  688. ETH_FUNC_RET_STATUS status;
  689. struct net_device_stats *stats;
  690. ETH_FUNC_RET_STATUS release_result;
  691. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  692. port_private =
  693. (struct mv64460_eth_priv *) ethernet_private->port_private;
  694. port_num = port_private->port_num;
  695. stats = port_private->stats;
  696. /* Update packet info data structure */
  697. pkt_info.cmd_sts = ETH_TX_FIRST_DESC | ETH_TX_LAST_DESC; /* DMA owned, first last */
  698. pkt_info.byte_cnt = dataSize;
  699. pkt_info.buf_ptr = (unsigned int) dataPtr;
  700. pkt_info.return_info = 0;
  701. status = eth_port_send (ethernet_private, ETH_Q0, &pkt_info);
  702. if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL)) {
  703. printf ("Error on transmitting packet ..");
  704. if (status == ETH_QUEUE_FULL)
  705. printf ("ETH Queue is full. \n");
  706. if (status == ETH_QUEUE_LAST_RESOURCE)
  707. printf ("ETH Queue: using last available resource. \n");
  708. return 1;
  709. }
  710. /* Update statistics and start of transmittion time */
  711. stats->tx_bytes += dataSize;
  712. stats->tx_packets++;
  713. /* Check if packet(s) is(are) transmitted correctly (release everything) */
  714. do {
  715. release_result =
  716. eth_tx_return_desc (ethernet_private, ETH_Q0,
  717. &pkt_info);
  718. switch (release_result) {
  719. case ETH_OK:
  720. DP (printf ("descriptor released\n"));
  721. if (pkt_info.cmd_sts & BIT0) {
  722. printf ("Error in TX\n");
  723. stats->tx_errors++;
  724. }
  725. break;
  726. case ETH_RETRY:
  727. DP (printf ("transmission still in process\n"));
  728. break;
  729. case ETH_ERROR:
  730. printf ("routine can not access Tx desc ring\n");
  731. break;
  732. case ETH_END_OF_JOB:
  733. DP (printf ("the routine has nothing to release\n"));
  734. break;
  735. default: /* should not happen */
  736. break;
  737. }
  738. } while (release_result == ETH_OK);
  739. return 0; /* success */
  740. }
  741. /**********************************************************************
  742. * mv64460_eth_receive
  743. *
  744. * This function is forward packets that are received from the port's
  745. * queues toward kernel core or FastRoute them to another interface.
  746. *
  747. * Input : dev - a pointer to the required interface
  748. * max - maximum number to receive (0 means unlimted)
  749. *
  750. * Output : number of served packets
  751. **********************************************************************/
  752. int mv64460_eth_receive (struct eth_device *dev)
  753. {
  754. ETH_PORT_INFO *ethernet_private;
  755. struct mv64460_eth_priv *port_private;
  756. unsigned int port_num;
  757. PKT_INFO pkt_info;
  758. struct net_device_stats *stats;
  759. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  760. port_private = (struct mv64460_eth_priv *) ethernet_private->port_private;
  761. port_num = port_private->port_num;
  762. stats = port_private->stats;
  763. while ((eth_port_receive (ethernet_private, ETH_Q0, &pkt_info) == ETH_OK)) {
  764. #ifdef DEBUG_MV_ETH
  765. if (pkt_info.byte_cnt != 0) {
  766. printf ("%s: Received %d byte Packet @ 0x%x\n",
  767. __FUNCTION__, pkt_info.byte_cnt,
  768. pkt_info.buf_ptr);
  769. if(pkt_info.buf_ptr != 0){
  770. for(i=0; i < pkt_info.byte_cnt; i++){
  771. if((i % 4) == 0){
  772. printf("\n0x");
  773. }
  774. printf("%02x", ((char*)pkt_info.buf_ptr)[i]);
  775. }
  776. printf("\n");
  777. }
  778. }
  779. #endif
  780. /* Update statistics. Note byte count includes 4 byte CRC count */
  781. stats->rx_packets++;
  782. stats->rx_bytes += pkt_info.byte_cnt;
  783. /*
  784. * In case received a packet without first / last bits on OR the error
  785. * summary bit is on, the packets needs to be dropeed.
  786. */
  787. if (((pkt_info.
  788. cmd_sts & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
  789. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
  790. || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
  791. stats->rx_dropped++;
  792. printf ("Received packet spread on multiple descriptors\n");
  793. /* Is this caused by an error ? */
  794. if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY) {
  795. stats->rx_errors++;
  796. }
  797. /* free these descriptors again without forwarding them to the higher layers */
  798. pkt_info.buf_ptr &= ~0x7; /* realign buffer again */
  799. pkt_info.byte_cnt = 0x0000; /* Reset Byte count */
  800. if (eth_rx_return_buff
  801. (ethernet_private, ETH_Q0, &pkt_info) != ETH_OK) {
  802. printf ("Error while returning the RX Desc to Ring\n");
  803. } else {
  804. DP (printf ("RX Desc returned to Ring\n"));
  805. }
  806. /* /free these descriptors again */
  807. } else {
  808. /* !!! call higher layer processing */
  809. #ifdef DEBUG_MV_ETH
  810. printf ("\nNow send it to upper layer protocols (NetReceive) ...\n");
  811. #endif
  812. /* let the upper layer handle the packet */
  813. NetReceive ((uchar *) pkt_info.buf_ptr,
  814. (int) pkt_info.byte_cnt);
  815. /* **************************************************************** */
  816. /* free descriptor */
  817. pkt_info.buf_ptr &= ~0x7; /* realign buffer again */
  818. pkt_info.byte_cnt = 0x0000; /* Reset Byte count */
  819. DP (printf ("RX: pkt_info.buf_ptr = %x\n", pkt_info.buf_ptr));
  820. if (eth_rx_return_buff
  821. (ethernet_private, ETH_Q0, &pkt_info) != ETH_OK) {
  822. printf ("Error while returning the RX Desc to Ring\n");
  823. } else {
  824. DP (printf ("RX: Desc returned to Ring\n"));
  825. }
  826. /* **************************************************************** */
  827. }
  828. }
  829. mv64460_eth_get_stats (dev); /* update statistics */
  830. return 1;
  831. }
  832. /**********************************************************************
  833. * mv64460_eth_get_stats
  834. *
  835. * Returns a pointer to the interface statistics.
  836. *
  837. * Input : dev - a pointer to the required interface
  838. *
  839. * Output : a pointer to the interface's statistics
  840. **********************************************************************/
  841. static struct net_device_stats *mv64460_eth_get_stats (struct eth_device *dev)
  842. {
  843. ETH_PORT_INFO *ethernet_private;
  844. struct mv64460_eth_priv *port_private;
  845. unsigned int port_num;
  846. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  847. port_private =
  848. (struct mv64460_eth_priv *) ethernet_private->port_private;
  849. port_num = port_private->port_num;
  850. mv64460_eth_update_stat (dev);
  851. return port_private->stats;
  852. }
  853. /**********************************************************************
  854. * mv64460_eth_update_stat
  855. *
  856. * Update the statistics structure in the private data structure
  857. *
  858. * Input : pointer to ethernet interface network device structure
  859. * Output : N/A
  860. **********************************************************************/
  861. static void mv64460_eth_update_stat (struct eth_device *dev)
  862. {
  863. ETH_PORT_INFO *ethernet_private;
  864. struct mv64460_eth_priv *port_private;
  865. struct net_device_stats *stats;
  866. unsigned int port_num;
  867. volatile unsigned int dummy;
  868. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  869. port_private =
  870. (struct mv64460_eth_priv *) ethernet_private->port_private;
  871. port_num = port_private->port_num;
  872. stats = port_private->stats;
  873. /* These are false updates */
  874. stats->rx_packets += (unsigned long)
  875. eth_read_mib_counter (ethernet_private->port_num,
  876. ETH_MIB_GOOD_FRAMES_RECEIVED);
  877. stats->tx_packets += (unsigned long)
  878. eth_read_mib_counter (ethernet_private->port_num,
  879. ETH_MIB_GOOD_FRAMES_SENT);
  880. stats->rx_bytes += (unsigned long)
  881. eth_read_mib_counter (ethernet_private->port_num,
  882. ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
  883. /*
  884. * Ideally this should be as follows -
  885. *
  886. * stats->rx_bytes += stats->rx_bytes +
  887. * ((unsigned long) ethReadMibCounter (ethernet_private->port_num ,
  888. * ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32);
  889. *
  890. * But the unsigned long in PowerPC and MIPS are 32bit. So the next read
  891. * is just a dummy read for proper work of the GigE port
  892. */
  893. dummy = eth_read_mib_counter (ethernet_private->port_num,
  894. ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH);
  895. stats->tx_bytes += (unsigned long)
  896. eth_read_mib_counter (ethernet_private->port_num,
  897. ETH_MIB_GOOD_OCTETS_SENT_LOW);
  898. dummy = eth_read_mib_counter (ethernet_private->port_num,
  899. ETH_MIB_GOOD_OCTETS_SENT_HIGH);
  900. stats->rx_errors += (unsigned long)
  901. eth_read_mib_counter (ethernet_private->port_num,
  902. ETH_MIB_MAC_RECEIVE_ERROR);
  903. /* Rx dropped is for received packet with CRC error */
  904. stats->rx_dropped +=
  905. (unsigned long) eth_read_mib_counter (ethernet_private->
  906. port_num,
  907. ETH_MIB_BAD_CRC_EVENT);
  908. stats->multicast += (unsigned long)
  909. eth_read_mib_counter (ethernet_private->port_num,
  910. ETH_MIB_MULTICAST_FRAMES_RECEIVED);
  911. stats->collisions +=
  912. (unsigned long) eth_read_mib_counter (ethernet_private->
  913. port_num,
  914. ETH_MIB_COLLISION) +
  915. (unsigned long) eth_read_mib_counter (ethernet_private->
  916. port_num,
  917. ETH_MIB_LATE_COLLISION);
  918. /* detailed rx errors */
  919. stats->rx_length_errors +=
  920. (unsigned long) eth_read_mib_counter (ethernet_private->
  921. port_num,
  922. ETH_MIB_UNDERSIZE_RECEIVED)
  923. +
  924. (unsigned long) eth_read_mib_counter (ethernet_private->
  925. port_num,
  926. ETH_MIB_OVERSIZE_RECEIVED);
  927. /* detailed tx errors */
  928. }
  929. #ifndef UPDATE_STATS_BY_SOFTWARE
  930. /**********************************************************************
  931. * mv64460_eth_print_stat
  932. *
  933. * Update the statistics structure in the private data structure
  934. *
  935. * Input : pointer to ethernet interface network device structure
  936. * Output : N/A
  937. **********************************************************************/
  938. static void mv64460_eth_print_stat (struct eth_device *dev)
  939. {
  940. ETH_PORT_INFO *ethernet_private;
  941. struct mv64460_eth_priv *port_private;
  942. struct net_device_stats *stats;
  943. unsigned int port_num;
  944. ethernet_private = (ETH_PORT_INFO *) dev->priv;
  945. port_private =
  946. (struct mv64460_eth_priv *) ethernet_private->port_private;
  947. port_num = port_private->port_num;
  948. stats = port_private->stats;
  949. /* These are false updates */
  950. printf ("\n### Network statistics: ###\n");
  951. printf ("--------------------------\n");
  952. printf (" Packets received: %ld\n", stats->rx_packets);
  953. printf (" Packets send: %ld\n", stats->tx_packets);
  954. printf (" Received bytes: %ld\n", stats->rx_bytes);
  955. printf (" Send bytes: %ld\n", stats->tx_bytes);
  956. if (stats->rx_errors != 0)
  957. printf (" Rx Errors: %ld\n",
  958. stats->rx_errors);
  959. if (stats->rx_dropped != 0)
  960. printf (" Rx dropped (CRC Errors): %ld\n",
  961. stats->rx_dropped);
  962. if (stats->multicast != 0)
  963. printf (" Rx mulicast frames: %ld\n",
  964. stats->multicast);
  965. if (stats->collisions != 0)
  966. printf (" No. of collisions: %ld\n",
  967. stats->collisions);
  968. if (stats->rx_length_errors != 0)
  969. printf (" Rx length errors: %ld\n",
  970. stats->rx_length_errors);
  971. }
  972. #endif
  973. /**************************************************************************
  974. *network_start - Network Kick Off Routine UBoot
  975. *Inputs :
  976. *Outputs :
  977. **************************************************************************/
  978. bool db64460_eth_start (struct eth_device *dev)
  979. {
  980. return (mv64460_eth_open (dev)); /* calls real open */
  981. }
  982. /*************************************************************************
  983. **************************************************************************
  984. **************************************************************************
  985. * The second part is the low level driver of the gigE ethernet ports. *
  986. **************************************************************************
  987. **************************************************************************
  988. *************************************************************************/
  989. /*
  990. * based on Linux code
  991. * arch/ppc/galileo/EVB64460/mv64460_eth.c - Driver for MV64460X ethernet ports
  992. * Copyright (C) 2002 rabeeh@galileo.co.il
  993. * This program is free software; you can redistribute it and/or
  994. * modify it under the terms of the GNU General Public License
  995. * as published by the Free Software Foundation; either version 2
  996. * of the License, or (at your option) any later version.
  997. * This program is distributed in the hope that it will be useful,
  998. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  999. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1000. * GNU General Public License for more details.
  1001. * You should have received a copy of the GNU General Public License
  1002. * along with this program; if not, write to the Free Software
  1003. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  1004. *
  1005. */
  1006. /********************************************************************************
  1007. * Marvell's Gigabit Ethernet controller low level driver
  1008. *
  1009. * DESCRIPTION:
  1010. * This file introduce low level API to Marvell's Gigabit Ethernet
  1011. * controller. This Gigabit Ethernet Controller driver API controls
  1012. * 1) Operations (i.e. port init, start, reset etc').
  1013. * 2) Data flow (i.e. port send, receive etc').
  1014. * Each Gigabit Ethernet port is controlled via ETH_PORT_INFO
  1015. * struct.
  1016. * This struct includes user configuration information as well as
  1017. * driver internal data needed for its operations.
  1018. *
  1019. * Supported Features:
  1020. * - This low level driver is OS independent. Allocating memory for
  1021. * the descriptor rings and buffers are not within the scope of
  1022. * this driver.
  1023. * - The user is free from Rx/Tx queue managing.
  1024. * - This low level driver introduce functionality API that enable
  1025. * the to operate Marvell's Gigabit Ethernet Controller in a
  1026. * convenient way.
  1027. * - Simple Gigabit Ethernet port operation API.
  1028. * - Simple Gigabit Ethernet port data flow API.
  1029. * - Data flow and operation API support per queue functionality.
  1030. * - Support cached descriptors for better performance.
  1031. * - Enable access to all four DRAM banks and internal SRAM memory
  1032. * spaces.
  1033. * - PHY access and control API.
  1034. * - Port control register configuration API.
  1035. * - Full control over Unicast and Multicast MAC configurations.
  1036. *
  1037. * Operation flow:
  1038. *
  1039. * Initialization phase
  1040. * This phase complete the initialization of the ETH_PORT_INFO
  1041. * struct.
  1042. * User information regarding port configuration has to be set
  1043. * prior to calling the port initialization routine. For example,
  1044. * the user has to assign the port_phy_addr field which is board
  1045. * depended parameter.
  1046. * In this phase any port Tx/Rx activity is halted, MIB counters
  1047. * are cleared, PHY address is set according to user parameter and
  1048. * access to DRAM and internal SRAM memory spaces.
  1049. *
  1050. * Driver ring initialization
  1051. * Allocating memory for the descriptor rings and buffers is not
  1052. * within the scope of this driver. Thus, the user is required to
  1053. * allocate memory for the descriptors ring and buffers. Those
  1054. * memory parameters are used by the Rx and Tx ring initialization
  1055. * routines in order to curve the descriptor linked list in a form
  1056. * of a ring.
  1057. * Note: Pay special attention to alignment issues when using
  1058. * cached descriptors/buffers. In this phase the driver store
  1059. * information in the ETH_PORT_INFO struct regarding each queue
  1060. * ring.
  1061. *
  1062. * Driver start
  1063. * This phase prepares the Ethernet port for Rx and Tx activity.
  1064. * It uses the information stored in the ETH_PORT_INFO struct to
  1065. * initialize the various port registers.
  1066. *
  1067. * Data flow:
  1068. * All packet references to/from the driver are done using PKT_INFO
  1069. * struct.
  1070. * This struct is a unified struct used with Rx and Tx operations.
  1071. * This way the user is not required to be familiar with neither
  1072. * Tx nor Rx descriptors structures.
  1073. * The driver's descriptors rings are management by indexes.
  1074. * Those indexes controls the ring resources and used to indicate
  1075. * a SW resource error:
  1076. * 'current'
  1077. * This index points to the current available resource for use. For
  1078. * example in Rx process this index will point to the descriptor
  1079. * that will be passed to the user upon calling the receive routine.
  1080. * In Tx process, this index will point to the descriptor
  1081. * that will be assigned with the user packet info and transmitted.
  1082. * 'used'
  1083. * This index points to the descriptor that need to restore its
  1084. * resources. For example in Rx process, using the Rx buffer return
  1085. * API will attach the buffer returned in packet info to the
  1086. * descriptor pointed by 'used'. In Tx process, using the Tx
  1087. * descriptor return will merely return the user packet info with
  1088. * the command status of the transmitted buffer pointed by the
  1089. * 'used' index. Nevertheless, it is essential to use this routine
  1090. * to update the 'used' index.
  1091. * 'first'
  1092. * This index supports Tx Scatter-Gather. It points to the first
  1093. * descriptor of a packet assembled of multiple buffers. For example
  1094. * when in middle of Such packet we have a Tx resource error the
  1095. * 'curr' index get the value of 'first' to indicate that the ring
  1096. * returned to its state before trying to transmit this packet.
  1097. *
  1098. * Receive operation:
  1099. * The eth_port_receive API set the packet information struct,
  1100. * passed by the caller, with received information from the
  1101. * 'current' SDMA descriptor.
  1102. * It is the user responsibility to return this resource back
  1103. * to the Rx descriptor ring to enable the reuse of this source.
  1104. * Return Rx resource is done using the eth_rx_return_buff API.
  1105. *
  1106. * Transmit operation:
  1107. * The eth_port_send API supports Scatter-Gather which enables to
  1108. * send a packet spanned over multiple buffers. This means that
  1109. * for each packet info structure given by the user and put into
  1110. * the Tx descriptors ring, will be transmitted only if the 'LAST'
  1111. * bit will be set in the packet info command status field. This
  1112. * API also consider restriction regarding buffer alignments and
  1113. * sizes.
  1114. * The user must return a Tx resource after ensuring the buffer
  1115. * has been transmitted to enable the Tx ring indexes to update.
  1116. *
  1117. * BOARD LAYOUT
  1118. * This device is on-board. No jumper diagram is necessary.
  1119. *
  1120. * EXTERNAL INTERFACE
  1121. *
  1122. * Prior to calling the initialization routine eth_port_init() the user
  1123. * must set the following fields under ETH_PORT_INFO struct:
  1124. * port_num User Ethernet port number.
  1125. * port_phy_addr User PHY address of Ethernet port.
  1126. * port_mac_addr[6] User defined port MAC address.
  1127. * port_config User port configuration value.
  1128. * port_config_extend User port config extend value.
  1129. * port_sdma_config User port SDMA config value.
  1130. * port_serial_control User port serial control value.
  1131. * *port_virt_to_phys () User function to cast virtual addr to CPU bus addr.
  1132. * *port_private User scratch pad for user specific data structures.
  1133. *
  1134. * This driver introduce a set of default values:
  1135. * PORT_CONFIG_VALUE Default port configuration value
  1136. * PORT_CONFIG_EXTEND_VALUE Default port extend configuration value
  1137. * PORT_SDMA_CONFIG_VALUE Default sdma control value
  1138. * PORT_SERIAL_CONTROL_VALUE Default port serial control value
  1139. *
  1140. * This driver data flow is done using the PKT_INFO struct which is
  1141. * a unified struct for Rx and Tx operations:
  1142. * byte_cnt Tx/Rx descriptor buffer byte count.
  1143. * l4i_chk CPU provided TCP Checksum. For Tx operation only.
  1144. * cmd_sts Tx/Rx descriptor command status.
  1145. * buf_ptr Tx/Rx descriptor buffer pointer.
  1146. * return_info Tx/Rx user resource return information.
  1147. *
  1148. *
  1149. * EXTERNAL SUPPORT REQUIREMENTS
  1150. *
  1151. * This driver requires the following external support:
  1152. *
  1153. * D_CACHE_FLUSH_LINE (address, address offset)
  1154. *
  1155. * This macro applies assembly code to flush and invalidate cache
  1156. * line.
  1157. * address - address base.
  1158. * address offset - address offset
  1159. *
  1160. *
  1161. * CPU_PIPE_FLUSH
  1162. *
  1163. * This macro applies assembly code to flush the CPU pipeline.
  1164. *
  1165. *******************************************************************************/
  1166. /* includes */
  1167. /* defines */
  1168. /* SDMA command macros */
  1169. #define ETH_ENABLE_TX_QUEUE(tx_queue, eth_port) \
  1170. MV_REG_WRITE(MV64460_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port), (1 << tx_queue))
  1171. #define ETH_DISABLE_TX_QUEUE(tx_queue, eth_port) \
  1172. MV_REG_WRITE(MV64460_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port),\
  1173. (1 << (8 + tx_queue)))
  1174. #define ETH_ENABLE_RX_QUEUE(rx_queue, eth_port) \
  1175. MV_REG_WRITE(MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG(eth_port), (1 << rx_queue))
  1176. #define ETH_DISABLE_RX_QUEUE(rx_queue, eth_port) \
  1177. MV_REG_WRITE(MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG(eth_port), (1 << (8 + rx_queue)))
  1178. #define CURR_RFD_GET(p_curr_desc, queue) \
  1179. ((p_curr_desc) = p_eth_port_ctrl->p_rx_curr_desc_q[queue])
  1180. #define CURR_RFD_SET(p_curr_desc, queue) \
  1181. (p_eth_port_ctrl->p_rx_curr_desc_q[queue] = (p_curr_desc))
  1182. #define USED_RFD_GET(p_used_desc, queue) \
  1183. ((p_used_desc) = p_eth_port_ctrl->p_rx_used_desc_q[queue])
  1184. #define USED_RFD_SET(p_used_desc, queue)\
  1185. (p_eth_port_ctrl->p_rx_used_desc_q[queue] = (p_used_desc))
  1186. #define CURR_TFD_GET(p_curr_desc, queue) \
  1187. ((p_curr_desc) = p_eth_port_ctrl->p_tx_curr_desc_q[queue])
  1188. #define CURR_TFD_SET(p_curr_desc, queue) \
  1189. (p_eth_port_ctrl->p_tx_curr_desc_q[queue] = (p_curr_desc))
  1190. #define USED_TFD_GET(p_used_desc, queue) \
  1191. ((p_used_desc) = p_eth_port_ctrl->p_tx_used_desc_q[queue])
  1192. #define USED_TFD_SET(p_used_desc, queue) \
  1193. (p_eth_port_ctrl->p_tx_used_desc_q[queue] = (p_used_desc))
  1194. #define FIRST_TFD_GET(p_first_desc, queue) \
  1195. ((p_first_desc) = p_eth_port_ctrl->p_tx_first_desc_q[queue])
  1196. #define FIRST_TFD_SET(p_first_desc, queue) \
  1197. (p_eth_port_ctrl->p_tx_first_desc_q[queue] = (p_first_desc))
  1198. /* Macros that save access to desc in order to find next desc pointer */
  1199. #define RX_NEXT_DESC_PTR(p_rx_desc, queue) (ETH_RX_DESC*)(((((unsigned int)p_rx_desc - (unsigned int)p_eth_port_ctrl->p_rx_desc_area_base[queue]) + RX_DESC_ALIGNED_SIZE) % p_eth_port_ctrl->rx_desc_area_size[queue]) + (unsigned int)p_eth_port_ctrl->p_rx_desc_area_base[queue])
  1200. #define TX_NEXT_DESC_PTR(p_tx_desc, queue) (ETH_TX_DESC*)(((((unsigned int)p_tx_desc - (unsigned int)p_eth_port_ctrl->p_tx_desc_area_base[queue]) + TX_DESC_ALIGNED_SIZE) % p_eth_port_ctrl->tx_desc_area_size[queue]) + (unsigned int)p_eth_port_ctrl->p_tx_desc_area_base[queue])
  1201. #define LINK_UP_TIMEOUT 100000
  1202. #define PHY_BUSY_TIMEOUT 10000000
  1203. /* locals */
  1204. /* PHY routines */
  1205. static void ethernet_phy_set (ETH_PORT eth_port_num, int phy_addr);
  1206. static int ethernet_phy_get (ETH_PORT eth_port_num);
  1207. /* Ethernet Port routines */
  1208. static void eth_set_access_control (ETH_PORT eth_port_num,
  1209. ETH_WIN_PARAM * param);
  1210. static bool eth_port_uc_addr (ETH_PORT eth_port_num, unsigned char uc_nibble,
  1211. ETH_QUEUE queue, int option);
  1212. #if 0 /* FIXME */
  1213. static bool eth_port_smc_addr (ETH_PORT eth_port_num,
  1214. unsigned char mc_byte,
  1215. ETH_QUEUE queue, int option);
  1216. static bool eth_port_omc_addr (ETH_PORT eth_port_num,
  1217. unsigned char crc8,
  1218. ETH_QUEUE queue, int option);
  1219. #endif
  1220. static void eth_b_copy (unsigned int src_addr, unsigned int dst_addr,
  1221. int byte_count);
  1222. void eth_dbg (ETH_PORT_INFO * p_eth_port_ctrl);
  1223. typedef enum _memory_bank { BANK0, BANK1, BANK2, BANK3 } MEMORY_BANK;
  1224. u32 mv_get_dram_bank_base_addr (MEMORY_BANK bank)
  1225. {
  1226. u32 result = 0;
  1227. u32 enable = MV_REG_READ (MV64460_BASE_ADDR_ENABLE);
  1228. if (enable & (1 << bank))
  1229. return 0;
  1230. if (bank == BANK0)
  1231. result = MV_REG_READ (MV64460_CS_0_BASE_ADDR);
  1232. if (bank == BANK1)
  1233. result = MV_REG_READ (MV64460_CS_1_BASE_ADDR);
  1234. if (bank == BANK2)
  1235. result = MV_REG_READ (MV64460_CS_2_BASE_ADDR);
  1236. if (bank == BANK3)
  1237. result = MV_REG_READ (MV64460_CS_3_BASE_ADDR);
  1238. result &= 0x0000ffff;
  1239. result = result << 16;
  1240. return result;
  1241. }
  1242. u32 mv_get_dram_bank_size (MEMORY_BANK bank)
  1243. {
  1244. u32 result = 0;
  1245. u32 enable = MV_REG_READ (MV64460_BASE_ADDR_ENABLE);
  1246. if (enable & (1 << bank))
  1247. return 0;
  1248. if (bank == BANK0)
  1249. result = MV_REG_READ (MV64460_CS_0_SIZE);
  1250. if (bank == BANK1)
  1251. result = MV_REG_READ (MV64460_CS_1_SIZE);
  1252. if (bank == BANK2)
  1253. result = MV_REG_READ (MV64460_CS_2_SIZE);
  1254. if (bank == BANK3)
  1255. result = MV_REG_READ (MV64460_CS_3_SIZE);
  1256. result += 1;
  1257. result &= 0x0000ffff;
  1258. result = result << 16;
  1259. return result;
  1260. }
  1261. u32 mv_get_internal_sram_base (void)
  1262. {
  1263. u32 result;
  1264. result = MV_REG_READ (MV64460_INTEGRATED_SRAM_BASE_ADDR);
  1265. result &= 0x0000ffff;
  1266. result = result << 16;
  1267. return result;
  1268. }
  1269. /*******************************************************************************
  1270. * eth_port_init - Initialize the Ethernet port driver
  1271. *
  1272. * DESCRIPTION:
  1273. * This function prepares the ethernet port to start its activity:
  1274. * 1) Completes the ethernet port driver struct initialization toward port
  1275. * start routine.
  1276. * 2) Resets the device to a quiescent state in case of warm reboot.
  1277. * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
  1278. * 4) Clean MAC tables. The reset status of those tables is unknown.
  1279. * 5) Set PHY address.
  1280. * Note: Call this routine prior to eth_port_start routine and after setting
  1281. * user values in the user fields of Ethernet port control struct (i.e.
  1282. * port_phy_addr).
  1283. *
  1284. * INPUT:
  1285. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet port control struct
  1286. *
  1287. * OUTPUT:
  1288. * See description.
  1289. *
  1290. * RETURN:
  1291. * None.
  1292. *
  1293. *******************************************************************************/
  1294. static void eth_port_init (ETH_PORT_INFO * p_eth_port_ctrl)
  1295. {
  1296. int queue;
  1297. ETH_WIN_PARAM win_param;
  1298. p_eth_port_ctrl->port_config = PORT_CONFIG_VALUE;
  1299. p_eth_port_ctrl->port_config_extend = PORT_CONFIG_EXTEND_VALUE;
  1300. p_eth_port_ctrl->port_sdma_config = PORT_SDMA_CONFIG_VALUE;
  1301. p_eth_port_ctrl->port_serial_control = PORT_SERIAL_CONTROL_VALUE;
  1302. p_eth_port_ctrl->port_rx_queue_command = 0;
  1303. p_eth_port_ctrl->port_tx_queue_command = 0;
  1304. /* Zero out SW structs */
  1305. for (queue = 0; queue < MAX_RX_QUEUE_NUM; queue++) {
  1306. CURR_RFD_SET ((ETH_RX_DESC *) 0x00000000, queue);
  1307. USED_RFD_SET ((ETH_RX_DESC *) 0x00000000, queue);
  1308. p_eth_port_ctrl->rx_resource_err[queue] = false;
  1309. }
  1310. for (queue = 0; queue < MAX_TX_QUEUE_NUM; queue++) {
  1311. CURR_TFD_SET ((ETH_TX_DESC *) 0x00000000, queue);
  1312. USED_TFD_SET ((ETH_TX_DESC *) 0x00000000, queue);
  1313. FIRST_TFD_SET ((ETH_TX_DESC *) 0x00000000, queue);
  1314. p_eth_port_ctrl->tx_resource_err[queue] = false;
  1315. }
  1316. eth_port_reset (p_eth_port_ctrl->port_num);
  1317. /* Set access parameters for DRAM bank 0 */
  1318. win_param.win = ETH_WIN0; /* Use Ethernet window 0 */
  1319. win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
  1320. win_param.attributes = EBAR_ATTR_DRAM_CS0; /* Enable DRAM bank */
  1321. #ifndef CONFIG_NOT_COHERENT_CACHE
  1322. win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
  1323. #endif
  1324. win_param.high_addr = 0;
  1325. /* Get bank base */
  1326. win_param.base_addr = mv_get_dram_bank_base_addr (BANK0);
  1327. win_param.size = mv_get_dram_bank_size (BANK0); /* Get bank size */
  1328. if (win_param.size == 0)
  1329. win_param.enable = 0;
  1330. else
  1331. win_param.enable = 1; /* Enable the access */
  1332. win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
  1333. /* Set the access control for address window (EPAPR) READ & WRITE */
  1334. eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
  1335. /* Set access parameters for DRAM bank 1 */
  1336. win_param.win = ETH_WIN1; /* Use Ethernet window 1 */
  1337. win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
  1338. win_param.attributes = EBAR_ATTR_DRAM_CS1; /* Enable DRAM bank */
  1339. #ifndef CONFIG_NOT_COHERENT_CACHE
  1340. win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
  1341. #endif
  1342. win_param.high_addr = 0;
  1343. /* Get bank base */
  1344. win_param.base_addr = mv_get_dram_bank_base_addr (BANK1);
  1345. win_param.size = mv_get_dram_bank_size (BANK1); /* Get bank size */
  1346. if (win_param.size == 0)
  1347. win_param.enable = 0;
  1348. else
  1349. win_param.enable = 1; /* Enable the access */
  1350. win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
  1351. /* Set the access control for address window (EPAPR) READ & WRITE */
  1352. eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
  1353. /* Set access parameters for DRAM bank 2 */
  1354. win_param.win = ETH_WIN2; /* Use Ethernet window 2 */
  1355. win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
  1356. win_param.attributes = EBAR_ATTR_DRAM_CS2; /* Enable DRAM bank */
  1357. #ifndef CONFIG_NOT_COHERENT_CACHE
  1358. win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
  1359. #endif
  1360. win_param.high_addr = 0;
  1361. /* Get bank base */
  1362. win_param.base_addr = mv_get_dram_bank_base_addr (BANK2);
  1363. win_param.size = mv_get_dram_bank_size (BANK2); /* Get bank size */
  1364. if (win_param.size == 0)
  1365. win_param.enable = 0;
  1366. else
  1367. win_param.enable = 1; /* Enable the access */
  1368. win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
  1369. /* Set the access control for address window (EPAPR) READ & WRITE */
  1370. eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
  1371. /* Set access parameters for DRAM bank 3 */
  1372. win_param.win = ETH_WIN3; /* Use Ethernet window 3 */
  1373. win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
  1374. win_param.attributes = EBAR_ATTR_DRAM_CS3; /* Enable DRAM bank */
  1375. #ifndef CONFIG_NOT_COHERENT_CACHE
  1376. win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
  1377. #endif
  1378. win_param.high_addr = 0;
  1379. /* Get bank base */
  1380. win_param.base_addr = mv_get_dram_bank_base_addr (BANK3);
  1381. win_param.size = mv_get_dram_bank_size (BANK3); /* Get bank size */
  1382. if (win_param.size == 0)
  1383. win_param.enable = 0;
  1384. else
  1385. win_param.enable = 1; /* Enable the access */
  1386. win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
  1387. /* Set the access control for address window (EPAPR) READ & WRITE */
  1388. eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
  1389. /* Set access parameters for Internal SRAM */
  1390. win_param.win = ETH_WIN4; /* Use Ethernet window 0 */
  1391. win_param.target = EBAR_TARGET_CBS; /* Target - Internal SRAM */
  1392. win_param.attributes = EBAR_ATTR_CBS_SRAM | EBAR_ATTR_CBS_SRAM_BLOCK0;
  1393. win_param.high_addr = 0;
  1394. win_param.base_addr = mv_get_internal_sram_base (); /* Get base addr */
  1395. win_param.size = MV64460_INTERNAL_SRAM_SIZE; /* Get bank size */
  1396. win_param.enable = 1; /* Enable the access */
  1397. win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
  1398. /* Set the access control for address window (EPAPR) READ & WRITE */
  1399. eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
  1400. eth_port_init_mac_tables (p_eth_port_ctrl->port_num);
  1401. ethernet_phy_set (p_eth_port_ctrl->port_num,
  1402. p_eth_port_ctrl->port_phy_addr);
  1403. return;
  1404. }
  1405. /*******************************************************************************
  1406. * eth_port_start - Start the Ethernet port activity.
  1407. *
  1408. * DESCRIPTION:
  1409. * This routine prepares the Ethernet port for Rx and Tx activity:
  1410. * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
  1411. * has been initialized a descriptor's ring (using ether_init_tx_desc_ring
  1412. * for Tx and ether_init_rx_desc_ring for Rx)
  1413. * 2. Initialize and enable the Ethernet configuration port by writing to
  1414. * the port's configuration and command registers.
  1415. * 3. Initialize and enable the SDMA by writing to the SDMA's
  1416. * configuration and command registers.
  1417. * After completing these steps, the ethernet port SDMA can starts to
  1418. * perform Rx and Tx activities.
  1419. *
  1420. * Note: Each Rx and Tx queue descriptor's list must be initialized prior
  1421. * to calling this function (use ether_init_tx_desc_ring for Tx queues and
  1422. * ether_init_rx_desc_ring for Rx queues).
  1423. *
  1424. * INPUT:
  1425. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet port control struct
  1426. *
  1427. * OUTPUT:
  1428. * Ethernet port is ready to receive and transmit.
  1429. *
  1430. * RETURN:
  1431. * false if the port PHY is not up.
  1432. * true otherwise.
  1433. *
  1434. *******************************************************************************/
  1435. static bool eth_port_start (ETH_PORT_INFO * p_eth_port_ctrl)
  1436. {
  1437. int queue;
  1438. volatile ETH_TX_DESC *p_tx_curr_desc;
  1439. volatile ETH_RX_DESC *p_rx_curr_desc;
  1440. unsigned int phy_reg_data;
  1441. ETH_PORT eth_port_num = p_eth_port_ctrl->port_num;
  1442. /* Assignment of Tx CTRP of given queue */
  1443. for (queue = 0; queue < MAX_TX_QUEUE_NUM; queue++) {
  1444. CURR_TFD_GET (p_tx_curr_desc, queue);
  1445. MV_REG_WRITE ((MV64460_ETH_TX_CURRENT_QUEUE_DESC_PTR_0
  1446. (eth_port_num)
  1447. + (4 * queue)),
  1448. ((unsigned int) p_tx_curr_desc));
  1449. }
  1450. /* Assignment of Rx CRDP of given queue */
  1451. for (queue = 0; queue < MAX_RX_QUEUE_NUM; queue++) {
  1452. CURR_RFD_GET (p_rx_curr_desc, queue);
  1453. MV_REG_WRITE ((MV64460_ETH_RX_CURRENT_QUEUE_DESC_PTR_0
  1454. (eth_port_num)
  1455. + (4 * queue)),
  1456. ((unsigned int) p_rx_curr_desc));
  1457. if (p_rx_curr_desc != NULL)
  1458. /* Add the assigned Ethernet address to the port's address table */
  1459. eth_port_uc_addr_set (p_eth_port_ctrl->port_num,
  1460. p_eth_port_ctrl->port_mac_addr,
  1461. queue);
  1462. }
  1463. /* Assign port configuration and command. */
  1464. MV_REG_WRITE (MV64460_ETH_PORT_CONFIG_REG (eth_port_num),
  1465. p_eth_port_ctrl->port_config);
  1466. MV_REG_WRITE (MV64460_ETH_PORT_CONFIG_EXTEND_REG (eth_port_num),
  1467. p_eth_port_ctrl->port_config_extend);
  1468. MV_REG_WRITE (MV64460_ETH_PORT_SERIAL_CONTROL_REG (eth_port_num),
  1469. p_eth_port_ctrl->port_serial_control);
  1470. MV_SET_REG_BITS (MV64460_ETH_PORT_SERIAL_CONTROL_REG (eth_port_num),
  1471. ETH_SERIAL_PORT_ENABLE);
  1472. /* Assign port SDMA configuration */
  1473. MV_REG_WRITE (MV64460_ETH_SDMA_CONFIG_REG (eth_port_num),
  1474. p_eth_port_ctrl->port_sdma_config);
  1475. MV_REG_WRITE (MV64460_ETH_TX_QUEUE_0_TOKEN_BUCKET_COUNT
  1476. (eth_port_num), 0x3fffffff);
  1477. MV_REG_WRITE (MV64460_ETH_TX_QUEUE_0_TOKEN_BUCKET_CONFIG
  1478. (eth_port_num), 0x03fffcff);
  1479. /* Turn off the port/queue bandwidth limitation */
  1480. MV_REG_WRITE (MV64460_ETH_MAXIMUM_TRANSMIT_UNIT (eth_port_num), 0x0);
  1481. /* Enable port Rx. */
  1482. MV_REG_WRITE (MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG (eth_port_num),
  1483. p_eth_port_ctrl->port_rx_queue_command);
  1484. /* Check if link is up */
  1485. eth_port_read_smi_reg (eth_port_num, 1, &phy_reg_data);
  1486. if (!(phy_reg_data & 0x20))
  1487. return false;
  1488. return true;
  1489. }
  1490. /*******************************************************************************
  1491. * eth_port_uc_addr_set - This function Set the port Unicast address.
  1492. *
  1493. * DESCRIPTION:
  1494. * This function Set the port Ethernet MAC address.
  1495. *
  1496. * INPUT:
  1497. * ETH_PORT eth_port_num Port number.
  1498. * char * p_addr Address to be set
  1499. * ETH_QUEUE queue Rx queue number for this MAC address.
  1500. *
  1501. * OUTPUT:
  1502. * Set MAC address low and high registers. also calls eth_port_uc_addr()
  1503. * To set the unicast table with the proper information.
  1504. *
  1505. * RETURN:
  1506. * N/A.
  1507. *
  1508. *******************************************************************************/
  1509. static void eth_port_uc_addr_set (ETH_PORT eth_port_num,
  1510. unsigned char *p_addr, ETH_QUEUE queue)
  1511. {
  1512. unsigned int mac_h;
  1513. unsigned int mac_l;
  1514. mac_l = (p_addr[4] << 8) | (p_addr[5]);
  1515. mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) |
  1516. (p_addr[2] << 8) | (p_addr[3] << 0);
  1517. MV_REG_WRITE (MV64460_ETH_MAC_ADDR_LOW (eth_port_num), mac_l);
  1518. MV_REG_WRITE (MV64460_ETH_MAC_ADDR_HIGH (eth_port_num), mac_h);
  1519. /* Accept frames of this address */
  1520. eth_port_uc_addr (eth_port_num, p_addr[5], queue, ACCEPT_MAC_ADDR);
  1521. return;
  1522. }
  1523. /*******************************************************************************
  1524. * eth_port_uc_addr - This function Set the port unicast address table
  1525. *
  1526. * DESCRIPTION:
  1527. * This function locates the proper entry in the Unicast table for the
  1528. * specified MAC nibble and sets its properties according to function
  1529. * parameters.
  1530. *
  1531. * INPUT:
  1532. * ETH_PORT eth_port_num Port number.
  1533. * unsigned char uc_nibble Unicast MAC Address last nibble.
  1534. * ETH_QUEUE queue Rx queue number for this MAC address.
  1535. * int option 0 = Add, 1 = remove address.
  1536. *
  1537. * OUTPUT:
  1538. * This function add/removes MAC addresses from the port unicast address
  1539. * table.
  1540. *
  1541. * RETURN:
  1542. * true is output succeeded.
  1543. * false if option parameter is invalid.
  1544. *
  1545. *******************************************************************************/
  1546. static bool eth_port_uc_addr (ETH_PORT eth_port_num,
  1547. unsigned char uc_nibble,
  1548. ETH_QUEUE queue, int option)
  1549. {
  1550. unsigned int unicast_reg;
  1551. unsigned int tbl_offset;
  1552. unsigned int reg_offset;
  1553. /* Locate the Unicast table entry */
  1554. uc_nibble = (0xf & uc_nibble);
  1555. tbl_offset = (uc_nibble / 4) * 4; /* Register offset from unicast table base */
  1556. reg_offset = uc_nibble % 4; /* Entry offset within the above register */
  1557. switch (option) {
  1558. case REJECT_MAC_ADDR:
  1559. /* Clear accepts frame bit at specified unicast DA table entry */
  1560. unicast_reg =
  1561. MV_REG_READ ((MV64460_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1562. (eth_port_num)
  1563. + tbl_offset));
  1564. unicast_reg &= (0x0E << (8 * reg_offset));
  1565. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1566. (eth_port_num)
  1567. + tbl_offset), unicast_reg);
  1568. break;
  1569. case ACCEPT_MAC_ADDR:
  1570. /* Set accepts frame bit at unicast DA filter table entry */
  1571. unicast_reg =
  1572. MV_REG_READ ((MV64460_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1573. (eth_port_num)
  1574. + tbl_offset));
  1575. unicast_reg |= ((0x01 | queue) << (8 * reg_offset));
  1576. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1577. (eth_port_num)
  1578. + tbl_offset), unicast_reg);
  1579. break;
  1580. default:
  1581. return false;
  1582. }
  1583. return true;
  1584. }
  1585. #if 0 /* FIXME */
  1586. /*******************************************************************************
  1587. * eth_port_mc_addr - Multicast address settings.
  1588. *
  1589. * DESCRIPTION:
  1590. * This API controls the MV device MAC multicast support.
  1591. * The MV device supports multicast using two tables:
  1592. * 1) Special Multicast Table for MAC addresses of the form
  1593. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_fF).
  1594. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  1595. * Table entries in the DA-Filter table.
  1596. * In this case, the function calls eth_port_smc_addr() routine to set the
  1597. * Special Multicast Table.
  1598. * 2) Other Multicast Table for multicast of another type. A CRC-8bit
  1599. * is used as an index to the Other Multicast Table entries in the
  1600. * DA-Filter table.
  1601. * In this case, the function calculates the CRC-8bit value and calls
  1602. * eth_port_omc_addr() routine to set the Other Multicast Table.
  1603. * INPUT:
  1604. * ETH_PORT eth_port_num Port number.
  1605. * unsigned char *p_addr Unicast MAC Address.
  1606. * ETH_QUEUE queue Rx queue number for this MAC address.
  1607. * int option 0 = Add, 1 = remove address.
  1608. *
  1609. * OUTPUT:
  1610. * See description.
  1611. *
  1612. * RETURN:
  1613. * true is output succeeded.
  1614. * false if add_address_table_entry( ) failed.
  1615. *
  1616. *******************************************************************************/
  1617. static void eth_port_mc_addr (ETH_PORT eth_port_num,
  1618. unsigned char *p_addr,
  1619. ETH_QUEUE queue, int option)
  1620. {
  1621. unsigned int mac_h;
  1622. unsigned int mac_l;
  1623. unsigned char crc_result = 0;
  1624. int mac_array[48];
  1625. int crc[8];
  1626. int i;
  1627. if ((p_addr[0] == 0x01) &&
  1628. (p_addr[1] == 0x00) &&
  1629. (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
  1630. eth_port_smc_addr (eth_port_num, p_addr[5], queue, option);
  1631. } else {
  1632. /* Calculate CRC-8 out of the given address */
  1633. mac_h = (p_addr[0] << 8) | (p_addr[1]);
  1634. mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
  1635. (p_addr[4] << 8) | (p_addr[5] << 0);
  1636. for (i = 0; i < 32; i++)
  1637. mac_array[i] = (mac_l >> i) & 0x1;
  1638. for (i = 32; i < 48; i++)
  1639. mac_array[i] = (mac_h >> (i - 32)) & 0x1;
  1640. crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^
  1641. mac_array[39] ^ mac_array[35] ^ mac_array[34] ^
  1642. mac_array[31] ^ mac_array[30] ^ mac_array[28] ^
  1643. mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
  1644. mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
  1645. mac_array[12] ^ mac_array[8] ^ mac_array[7] ^
  1646. mac_array[6] ^ mac_array[0];
  1647. crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^
  1648. mac_array[43] ^ mac_array[41] ^ mac_array[39] ^
  1649. mac_array[36] ^ mac_array[34] ^ mac_array[32] ^
  1650. mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
  1651. mac_array[24] ^ mac_array[23] ^ mac_array[22] ^
  1652. mac_array[21] ^ mac_array[20] ^ mac_array[18] ^
  1653. mac_array[17] ^ mac_array[16] ^ mac_array[15] ^
  1654. mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
  1655. mac_array[9] ^ mac_array[6] ^ mac_array[1] ^
  1656. mac_array[0];
  1657. crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^
  1658. mac_array[43] ^ mac_array[42] ^ mac_array[39] ^
  1659. mac_array[37] ^ mac_array[34] ^ mac_array[33] ^
  1660. mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
  1661. mac_array[24] ^ mac_array[22] ^ mac_array[17] ^
  1662. mac_array[15] ^ mac_array[13] ^ mac_array[12] ^
  1663. mac_array[10] ^ mac_array[8] ^ mac_array[6] ^
  1664. mac_array[2] ^ mac_array[1] ^ mac_array[0];
  1665. crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^
  1666. mac_array[43] ^ mac_array[40] ^ mac_array[38] ^
  1667. mac_array[35] ^ mac_array[34] ^ mac_array[30] ^
  1668. mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
  1669. mac_array[23] ^ mac_array[18] ^ mac_array[16] ^
  1670. mac_array[14] ^ mac_array[13] ^ mac_array[11] ^
  1671. mac_array[9] ^ mac_array[7] ^ mac_array[3] ^
  1672. mac_array[2] ^ mac_array[1];
  1673. crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^
  1674. mac_array[41] ^ mac_array[39] ^ mac_array[36] ^
  1675. mac_array[35] ^ mac_array[31] ^ mac_array[30] ^
  1676. mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
  1677. mac_array[19] ^ mac_array[17] ^ mac_array[15] ^
  1678. mac_array[14] ^ mac_array[12] ^ mac_array[10] ^
  1679. mac_array[8] ^ mac_array[4] ^ mac_array[3] ^
  1680. mac_array[2];
  1681. crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^
  1682. mac_array[42] ^ mac_array[40] ^ mac_array[37] ^
  1683. mac_array[36] ^ mac_array[32] ^ mac_array[31] ^
  1684. mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
  1685. mac_array[20] ^ mac_array[18] ^ mac_array[16] ^
  1686. mac_array[15] ^ mac_array[13] ^ mac_array[11] ^
  1687. mac_array[9] ^ mac_array[5] ^ mac_array[4] ^
  1688. mac_array[3];
  1689. crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^
  1690. mac_array[41] ^ mac_array[38] ^ mac_array[37] ^
  1691. mac_array[33] ^ mac_array[32] ^ mac_array[29] ^
  1692. mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
  1693. mac_array[19] ^ mac_array[17] ^ mac_array[16] ^
  1694. mac_array[14] ^ mac_array[12] ^ mac_array[10] ^
  1695. mac_array[6] ^ mac_array[5] ^ mac_array[4];
  1696. crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^
  1697. mac_array[39] ^ mac_array[38] ^ mac_array[34] ^
  1698. mac_array[33] ^ mac_array[30] ^ mac_array[29] ^
  1699. mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
  1700. mac_array[18] ^ mac_array[17] ^ mac_array[15] ^
  1701. mac_array[13] ^ mac_array[11] ^ mac_array[7] ^
  1702. mac_array[6] ^ mac_array[5];
  1703. for (i = 0; i < 8; i++)
  1704. crc_result = crc_result | (crc[i] << i);
  1705. eth_port_omc_addr (eth_port_num, crc_result, queue, option);
  1706. }
  1707. return;
  1708. }
  1709. /*******************************************************************************
  1710. * eth_port_smc_addr - Special Multicast address settings.
  1711. *
  1712. * DESCRIPTION:
  1713. * This routine controls the MV device special MAC multicast support.
  1714. * The Special Multicast Table for MAC addresses supports MAC of the form
  1715. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_fF).
  1716. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  1717. * Table entries in the DA-Filter table.
  1718. * This function set the Special Multicast Table appropriate entry
  1719. * according to the argument given.
  1720. *
  1721. * INPUT:
  1722. * ETH_PORT eth_port_num Port number.
  1723. * unsigned char mc_byte Multicast addr last byte (MAC DA[7:0] bits).
  1724. * ETH_QUEUE queue Rx queue number for this MAC address.
  1725. * int option 0 = Add, 1 = remove address.
  1726. *
  1727. * OUTPUT:
  1728. * See description.
  1729. *
  1730. * RETURN:
  1731. * true is output succeeded.
  1732. * false if option parameter is invalid.
  1733. *
  1734. *******************************************************************************/
  1735. static bool eth_port_smc_addr (ETH_PORT eth_port_num,
  1736. unsigned char mc_byte,
  1737. ETH_QUEUE queue, int option)
  1738. {
  1739. unsigned int smc_table_reg;
  1740. unsigned int tbl_offset;
  1741. unsigned int reg_offset;
  1742. /* Locate the SMC table entry */
  1743. tbl_offset = (mc_byte / 4) * 4; /* Register offset from SMC table base */
  1744. reg_offset = mc_byte % 4; /* Entry offset within the above register */
  1745. queue &= 0x7;
  1746. switch (option) {
  1747. case REJECT_MAC_ADDR:
  1748. /* Clear accepts frame bit at specified Special DA table entry */
  1749. smc_table_reg =
  1750. MV_REG_READ ((MV64460_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
  1751. smc_table_reg &= (0x0E << (8 * reg_offset));
  1752. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), smc_table_reg);
  1753. break;
  1754. case ACCEPT_MAC_ADDR:
  1755. /* Set accepts frame bit at specified Special DA table entry */
  1756. smc_table_reg =
  1757. MV_REG_READ ((MV64460_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
  1758. smc_table_reg |= ((0x01 | queue) << (8 * reg_offset));
  1759. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), smc_table_reg);
  1760. break;
  1761. default:
  1762. return false;
  1763. }
  1764. return true;
  1765. }
  1766. /*******************************************************************************
  1767. * eth_port_omc_addr - Multicast address settings.
  1768. *
  1769. * DESCRIPTION:
  1770. * This routine controls the MV device Other MAC multicast support.
  1771. * The Other Multicast Table is used for multicast of another type.
  1772. * A CRC-8bit is used as an index to the Other Multicast Table entries
  1773. * in the DA-Filter table.
  1774. * The function gets the CRC-8bit value from the calling routine and
  1775. * set the Other Multicast Table appropriate entry according to the
  1776. * CRC-8 argument given.
  1777. *
  1778. * INPUT:
  1779. * ETH_PORT eth_port_num Port number.
  1780. * unsigned char crc8 A CRC-8bit (Polynomial: x^8+x^2+x^1+1).
  1781. * ETH_QUEUE queue Rx queue number for this MAC address.
  1782. * int option 0 = Add, 1 = remove address.
  1783. *
  1784. * OUTPUT:
  1785. * See description.
  1786. *
  1787. * RETURN:
  1788. * true is output succeeded.
  1789. * false if option parameter is invalid.
  1790. *
  1791. *******************************************************************************/
  1792. static bool eth_port_omc_addr (ETH_PORT eth_port_num,
  1793. unsigned char crc8,
  1794. ETH_QUEUE queue, int option)
  1795. {
  1796. unsigned int omc_table_reg;
  1797. unsigned int tbl_offset;
  1798. unsigned int reg_offset;
  1799. /* Locate the OMC table entry */
  1800. tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
  1801. reg_offset = crc8 % 4; /* Entry offset within the above register */
  1802. queue &= 0x7;
  1803. switch (option) {
  1804. case REJECT_MAC_ADDR:
  1805. /* Clear accepts frame bit at specified Other DA table entry */
  1806. omc_table_reg =
  1807. MV_REG_READ ((MV64460_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
  1808. omc_table_reg &= (0x0E << (8 * reg_offset));
  1809. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), omc_table_reg);
  1810. break;
  1811. case ACCEPT_MAC_ADDR:
  1812. /* Set accepts frame bit at specified Other DA table entry */
  1813. omc_table_reg =
  1814. MV_REG_READ ((MV64460_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
  1815. omc_table_reg |= ((0x01 | queue) << (8 * reg_offset));
  1816. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), omc_table_reg);
  1817. break;
  1818. default:
  1819. return false;
  1820. }
  1821. return true;
  1822. }
  1823. #endif
  1824. /*******************************************************************************
  1825. * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
  1826. *
  1827. * DESCRIPTION:
  1828. * Go through all the DA filter tables (Unicast, Special Multicast & Other
  1829. * Multicast) and set each entry to 0.
  1830. *
  1831. * INPUT:
  1832. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  1833. *
  1834. * OUTPUT:
  1835. * Multicast and Unicast packets are rejected.
  1836. *
  1837. * RETURN:
  1838. * None.
  1839. *
  1840. *******************************************************************************/
  1841. static void eth_port_init_mac_tables (ETH_PORT eth_port_num)
  1842. {
  1843. int table_index;
  1844. /* Clear DA filter unicast table (Ex_dFUT) */
  1845. for (table_index = 0; table_index <= 0xC; table_index += 4)
  1846. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_UNICAST_TABLE_BASE
  1847. (eth_port_num) + table_index), 0);
  1848. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  1849. /* Clear DA filter special multicast table (Ex_dFSMT) */
  1850. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + table_index), 0);
  1851. /* Clear DA filter other multicast table (Ex_dFOMT) */
  1852. MV_REG_WRITE ((MV64460_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + table_index), 0);
  1853. }
  1854. }
  1855. /*******************************************************************************
  1856. * eth_clear_mib_counters - Clear all MIB counters
  1857. *
  1858. * DESCRIPTION:
  1859. * This function clears all MIB counters of a specific ethernet port.
  1860. * A read from the MIB counter will reset the counter.
  1861. *
  1862. * INPUT:
  1863. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  1864. *
  1865. * OUTPUT:
  1866. * After reading all MIB counters, the counters resets.
  1867. *
  1868. * RETURN:
  1869. * MIB counter value.
  1870. *
  1871. *******************************************************************************/
  1872. static void eth_clear_mib_counters (ETH_PORT eth_port_num)
  1873. {
  1874. int i;
  1875. unsigned int dummy;
  1876. /* Perform dummy reads from MIB counters */
  1877. for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
  1878. i += 4)
  1879. dummy = MV_REG_READ ((MV64460_ETH_MIB_COUNTERS_BASE
  1880. (eth_port_num) + i));
  1881. return;
  1882. }
  1883. /*******************************************************************************
  1884. * eth_read_mib_counter - Read a MIB counter
  1885. *
  1886. * DESCRIPTION:
  1887. * This function reads a MIB counter of a specific ethernet port.
  1888. * NOTE - If read from ETH_MIB_GOOD_OCTETS_RECEIVED_LOW, then the
  1889. * following read must be from ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH
  1890. * register. The same applies for ETH_MIB_GOOD_OCTETS_SENT_LOW and
  1891. * ETH_MIB_GOOD_OCTETS_SENT_HIGH
  1892. *
  1893. * INPUT:
  1894. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  1895. * unsigned int mib_offset MIB counter offset (use ETH_MIB_... macros).
  1896. *
  1897. * OUTPUT:
  1898. * After reading the MIB counter, the counter resets.
  1899. *
  1900. * RETURN:
  1901. * MIB counter value.
  1902. *
  1903. *******************************************************************************/
  1904. unsigned int eth_read_mib_counter (ETH_PORT eth_port_num,
  1905. unsigned int mib_offset)
  1906. {
  1907. return (MV_REG_READ (MV64460_ETH_MIB_COUNTERS_BASE (eth_port_num)
  1908. + mib_offset));
  1909. }
  1910. /*******************************************************************************
  1911. * ethernet_phy_set - Set the ethernet port PHY address.
  1912. *
  1913. * DESCRIPTION:
  1914. * This routine set the ethernet port PHY address according to given
  1915. * parameter.
  1916. *
  1917. * INPUT:
  1918. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  1919. *
  1920. * OUTPUT:
  1921. * Set PHY Address Register with given PHY address parameter.
  1922. *
  1923. * RETURN:
  1924. * None.
  1925. *
  1926. *******************************************************************************/
  1927. static void ethernet_phy_set (ETH_PORT eth_port_num, int phy_addr)
  1928. {
  1929. unsigned int reg_data;
  1930. reg_data = MV_REG_READ (MV64460_ETH_PHY_ADDR_REG);
  1931. reg_data &= ~(0x1F << (5 * eth_port_num));
  1932. reg_data |= (phy_addr << (5 * eth_port_num));
  1933. MV_REG_WRITE (MV64460_ETH_PHY_ADDR_REG, reg_data);
  1934. return;
  1935. }
  1936. /*******************************************************************************
  1937. * ethernet_phy_get - Get the ethernet port PHY address.
  1938. *
  1939. * DESCRIPTION:
  1940. * This routine returns the given ethernet port PHY address.
  1941. *
  1942. * INPUT:
  1943. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  1944. *
  1945. * OUTPUT:
  1946. * None.
  1947. *
  1948. * RETURN:
  1949. * PHY address.
  1950. *
  1951. *******************************************************************************/
  1952. static int ethernet_phy_get (ETH_PORT eth_port_num)
  1953. {
  1954. unsigned int reg_data;
  1955. reg_data = MV_REG_READ (MV64460_ETH_PHY_ADDR_REG);
  1956. return ((reg_data >> (5 * eth_port_num)) & 0x1f);
  1957. }
  1958. /***********************************************************/
  1959. /* (Re)start autonegotiation */
  1960. /***********************************************************/
  1961. int phy_setup_aneg (char *devname, unsigned char addr)
  1962. {
  1963. unsigned short ctl, adv;
  1964. /* Setup standard advertise */
  1965. miiphy_read (devname, addr, PHY_ANAR, &adv);
  1966. adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 |
  1967. PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
  1968. PHY_ANLPAR_10);
  1969. miiphy_write (devname, addr, PHY_ANAR, adv);
  1970. miiphy_read (devname, addr, PHY_1000BTCR, &adv);
  1971. adv |= (0x0300);
  1972. miiphy_write (devname, addr, PHY_1000BTCR, adv);
  1973. /* Start/Restart aneg */
  1974. miiphy_read (devname, addr, PHY_BMCR, &ctl);
  1975. ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  1976. miiphy_write (devname, addr, PHY_BMCR, ctl);
  1977. return 0;
  1978. }
  1979. /*******************************************************************************
  1980. * ethernet_phy_reset - Reset Ethernet port PHY.
  1981. *
  1982. * DESCRIPTION:
  1983. * This routine utilize the SMI interface to reset the ethernet port PHY.
  1984. * The routine waits until the link is up again or link up is timeout.
  1985. *
  1986. * INPUT:
  1987. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  1988. *
  1989. * OUTPUT:
  1990. * The ethernet port PHY renew its link.
  1991. *
  1992. * RETURN:
  1993. * None.
  1994. *
  1995. *******************************************************************************/
  1996. static bool ethernet_phy_reset (ETH_PORT eth_port_num)
  1997. {
  1998. unsigned int time_out = 50;
  1999. unsigned int phy_reg_data;
  2000. eth_port_read_smi_reg (eth_port_num, 20, &phy_reg_data);
  2001. phy_reg_data |= 0x0083; /* Set bit 7 to 1 for different RGMII timing */
  2002. eth_port_write_smi_reg (eth_port_num, 20, phy_reg_data);
  2003. /* Reset the PHY */
  2004. eth_port_read_smi_reg (eth_port_num, 0, &phy_reg_data);
  2005. phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
  2006. eth_port_write_smi_reg (eth_port_num, 0, phy_reg_data);
  2007. /* Poll on the PHY LINK */
  2008. do {
  2009. eth_port_read_smi_reg (eth_port_num, 1, &phy_reg_data);
  2010. if (time_out-- == 0)
  2011. return false;
  2012. }
  2013. while (!(phy_reg_data & 0x20));
  2014. return true;
  2015. }
  2016. /*******************************************************************************
  2017. * eth_port_reset - Reset Ethernet port
  2018. *
  2019. * DESCRIPTION:
  2020. * This routine resets the chip by aborting any SDMA engine activity and
  2021. * clearing the MIB counters. The Receiver and the Transmit unit are in
  2022. * idle state after this command is performed and the port is disabled.
  2023. *
  2024. * INPUT:
  2025. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2026. *
  2027. * OUTPUT:
  2028. * Channel activity is halted.
  2029. *
  2030. * RETURN:
  2031. * None.
  2032. *
  2033. *******************************************************************************/
  2034. static void eth_port_reset (ETH_PORT eth_port_num)
  2035. {
  2036. unsigned int reg_data;
  2037. /* Stop Tx port activity. Check port Tx activity. */
  2038. reg_data =
  2039. MV_REG_READ (MV64460_ETH_TRANSMIT_QUEUE_COMMAND_REG
  2040. (eth_port_num));
  2041. if (reg_data & 0xFF) {
  2042. /* Issue stop command for active channels only */
  2043. MV_REG_WRITE (MV64460_ETH_TRANSMIT_QUEUE_COMMAND_REG
  2044. (eth_port_num), (reg_data << 8));
  2045. /* Wait for all Tx activity to terminate. */
  2046. do {
  2047. /* Check port cause register that all Tx queues are stopped */
  2048. reg_data =
  2049. MV_REG_READ
  2050. (MV64460_ETH_TRANSMIT_QUEUE_COMMAND_REG
  2051. (eth_port_num));
  2052. }
  2053. while (reg_data & 0xFF);
  2054. }
  2055. /* Stop Rx port activity. Check port Rx activity. */
  2056. reg_data =
  2057. MV_REG_READ (MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG
  2058. (eth_port_num));
  2059. if (reg_data & 0xFF) {
  2060. /* Issue stop command for active channels only */
  2061. MV_REG_WRITE (MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG
  2062. (eth_port_num), (reg_data << 8));
  2063. /* Wait for all Rx activity to terminate. */
  2064. do {
  2065. /* Check port cause register that all Rx queues are stopped */
  2066. reg_data =
  2067. MV_REG_READ
  2068. (MV64460_ETH_RECEIVE_QUEUE_COMMAND_REG
  2069. (eth_port_num));
  2070. }
  2071. while (reg_data & 0xFF);
  2072. }
  2073. /* Clear all MIB counters */
  2074. eth_clear_mib_counters (eth_port_num);
  2075. /* Reset the Enable bit in the Configuration Register */
  2076. reg_data =
  2077. MV_REG_READ (MV64460_ETH_PORT_SERIAL_CONTROL_REG
  2078. (eth_port_num));
  2079. reg_data &= ~ETH_SERIAL_PORT_ENABLE;
  2080. MV_REG_WRITE (MV64460_ETH_PORT_SERIAL_CONTROL_REG (eth_port_num),
  2081. reg_data);
  2082. return;
  2083. }
  2084. #if 0 /* Not needed here */
  2085. /*******************************************************************************
  2086. * ethernet_set_config_reg - Set specified bits in configuration register.
  2087. *
  2088. * DESCRIPTION:
  2089. * This function sets specified bits in the given ethernet
  2090. * configuration register.
  2091. *
  2092. * INPUT:
  2093. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2094. * unsigned int value 32 bit value.
  2095. *
  2096. * OUTPUT:
  2097. * The set bits in the value parameter are set in the configuration
  2098. * register.
  2099. *
  2100. * RETURN:
  2101. * None.
  2102. *
  2103. *******************************************************************************/
  2104. static void ethernet_set_config_reg (ETH_PORT eth_port_num,
  2105. unsigned int value)
  2106. {
  2107. unsigned int eth_config_reg;
  2108. eth_config_reg =
  2109. MV_REG_READ (MV64460_ETH_PORT_CONFIG_REG (eth_port_num));
  2110. eth_config_reg |= value;
  2111. MV_REG_WRITE (MV64460_ETH_PORT_CONFIG_REG (eth_port_num),
  2112. eth_config_reg);
  2113. return;
  2114. }
  2115. #endif
  2116. #if 0 /* FIXME */
  2117. /*******************************************************************************
  2118. * ethernet_reset_config_reg - Reset specified bits in configuration register.
  2119. *
  2120. * DESCRIPTION:
  2121. * This function resets specified bits in the given Ethernet
  2122. * configuration register.
  2123. *
  2124. * INPUT:
  2125. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2126. * unsigned int value 32 bit value.
  2127. *
  2128. * OUTPUT:
  2129. * The set bits in the value parameter are reset in the configuration
  2130. * register.
  2131. *
  2132. * RETURN:
  2133. * None.
  2134. *
  2135. *******************************************************************************/
  2136. static void ethernet_reset_config_reg (ETH_PORT eth_port_num,
  2137. unsigned int value)
  2138. {
  2139. unsigned int eth_config_reg;
  2140. eth_config_reg = MV_REG_READ (MV64460_ETH_PORT_CONFIG_EXTEND_REG
  2141. (eth_port_num));
  2142. eth_config_reg &= ~value;
  2143. MV_REG_WRITE (MV64460_ETH_PORT_CONFIG_EXTEND_REG (eth_port_num),
  2144. eth_config_reg);
  2145. return;
  2146. }
  2147. #endif
  2148. #if 0 /* Not needed here */
  2149. /*******************************************************************************
  2150. * ethernet_get_config_reg - Get the port configuration register
  2151. *
  2152. * DESCRIPTION:
  2153. * This function returns the configuration register value of the given
  2154. * ethernet port.
  2155. *
  2156. * INPUT:
  2157. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2158. *
  2159. * OUTPUT:
  2160. * None.
  2161. *
  2162. * RETURN:
  2163. * Port configuration register value.
  2164. *
  2165. *******************************************************************************/
  2166. static unsigned int ethernet_get_config_reg (ETH_PORT eth_port_num)
  2167. {
  2168. unsigned int eth_config_reg;
  2169. eth_config_reg = MV_REG_READ (MV64460_ETH_PORT_CONFIG_EXTEND_REG
  2170. (eth_port_num));
  2171. return eth_config_reg;
  2172. }
  2173. #endif
  2174. /*******************************************************************************
  2175. * eth_port_read_smi_reg - Read PHY registers
  2176. *
  2177. * DESCRIPTION:
  2178. * This routine utilize the SMI interface to interact with the PHY in
  2179. * order to perform PHY register read.
  2180. *
  2181. * INPUT:
  2182. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2183. * unsigned int phy_reg PHY register address offset.
  2184. * unsigned int *value Register value buffer.
  2185. *
  2186. * OUTPUT:
  2187. * Write the value of a specified PHY register into given buffer.
  2188. *
  2189. * RETURN:
  2190. * false if the PHY is busy or read data is not in valid state.
  2191. * true otherwise.
  2192. *
  2193. *******************************************************************************/
  2194. static bool eth_port_read_smi_reg (ETH_PORT eth_port_num,
  2195. unsigned int phy_reg, unsigned int *value)
  2196. {
  2197. unsigned int reg_value;
  2198. unsigned int time_out = PHY_BUSY_TIMEOUT;
  2199. int phy_addr;
  2200. phy_addr = ethernet_phy_get (eth_port_num);
  2201. /* first check that it is not busy */
  2202. do {
  2203. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2204. if (time_out-- == 0) {
  2205. return false;
  2206. }
  2207. }
  2208. while (reg_value & ETH_SMI_BUSY);
  2209. /* not busy */
  2210. MV_REG_WRITE (MV64460_ETH_SMI_REG,
  2211. (phy_addr << 16) | (phy_reg << 21) |
  2212. ETH_SMI_OPCODE_READ);
  2213. time_out = PHY_BUSY_TIMEOUT; /* initialize the time out var again */
  2214. do {
  2215. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2216. if (time_out-- == 0) {
  2217. return false;
  2218. }
  2219. }
  2220. while ((reg_value & ETH_SMI_READ_VALID) != ETH_SMI_READ_VALID); /* Bit set equ operation done */
  2221. /* Wait for the data to update in the SMI register */
  2222. #define PHY_UPDATE_TIMEOUT 10000
  2223. for (time_out = 0; time_out < PHY_UPDATE_TIMEOUT; time_out++);
  2224. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2225. *value = reg_value & 0xffff;
  2226. return true;
  2227. }
  2228. int mv_miiphy_read(char *devname, unsigned char phy_addr,
  2229. unsigned char phy_reg, unsigned short *value)
  2230. {
  2231. unsigned int reg_value;
  2232. unsigned int time_out = PHY_BUSY_TIMEOUT;
  2233. /* first check that it is not busy */
  2234. do {
  2235. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2236. if (time_out-- == 0) {
  2237. return false;
  2238. }
  2239. }
  2240. while (reg_value & ETH_SMI_BUSY);
  2241. /* not busy */
  2242. MV_REG_WRITE (MV64460_ETH_SMI_REG,
  2243. (phy_addr << 16) | (phy_reg << 21) |
  2244. ETH_SMI_OPCODE_READ);
  2245. time_out = PHY_BUSY_TIMEOUT; /* initialize the time out var again */
  2246. do {
  2247. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2248. if (time_out-- == 0) {
  2249. return false;
  2250. }
  2251. }
  2252. while ((reg_value & ETH_SMI_READ_VALID) != ETH_SMI_READ_VALID); /* Bit set equ operation done */
  2253. /* Wait for the data to update in the SMI register */
  2254. for (time_out = 0; time_out < PHY_UPDATE_TIMEOUT; time_out++);
  2255. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2256. *value = reg_value & 0xffff;
  2257. return 0;
  2258. }
  2259. /*******************************************************************************
  2260. * eth_port_write_smi_reg - Write to PHY registers
  2261. *
  2262. * DESCRIPTION:
  2263. * This routine utilize the SMI interface to interact with the PHY in
  2264. * order to perform writes to PHY registers.
  2265. *
  2266. * INPUT:
  2267. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2268. * unsigned int phy_reg PHY register address offset.
  2269. * unsigned int value Register value.
  2270. *
  2271. * OUTPUT:
  2272. * Write the given value to the specified PHY register.
  2273. *
  2274. * RETURN:
  2275. * false if the PHY is busy.
  2276. * true otherwise.
  2277. *
  2278. *******************************************************************************/
  2279. static bool eth_port_write_smi_reg (ETH_PORT eth_port_num,
  2280. unsigned int phy_reg, unsigned int value)
  2281. {
  2282. unsigned int reg_value;
  2283. unsigned int time_out = PHY_BUSY_TIMEOUT;
  2284. int phy_addr;
  2285. phy_addr = ethernet_phy_get (eth_port_num);
  2286. /* first check that it is not busy */
  2287. do {
  2288. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2289. if (time_out-- == 0) {
  2290. return false;
  2291. }
  2292. }
  2293. while (reg_value & ETH_SMI_BUSY);
  2294. /* not busy */
  2295. MV_REG_WRITE (MV64460_ETH_SMI_REG,
  2296. (phy_addr << 16) | (phy_reg << 21) |
  2297. ETH_SMI_OPCODE_WRITE | (value & 0xffff));
  2298. return true;
  2299. }
  2300. int mv_miiphy_write(char *devname, unsigned char phy_addr,
  2301. unsigned char phy_reg, unsigned short value)
  2302. {
  2303. unsigned int reg_value;
  2304. unsigned int time_out = PHY_BUSY_TIMEOUT;
  2305. /* first check that it is not busy */
  2306. do {
  2307. reg_value = MV_REG_READ (MV64460_ETH_SMI_REG);
  2308. if (time_out-- == 0) {
  2309. return false;
  2310. }
  2311. }
  2312. while (reg_value & ETH_SMI_BUSY);
  2313. /* not busy */
  2314. MV_REG_WRITE (MV64460_ETH_SMI_REG,
  2315. (phy_addr << 16) | (phy_reg << 21) |
  2316. ETH_SMI_OPCODE_WRITE | (value & 0xffff));
  2317. return 0;
  2318. }
  2319. /*******************************************************************************
  2320. * eth_set_access_control - Config address decode parameters for Ethernet unit
  2321. *
  2322. * DESCRIPTION:
  2323. * This function configures the address decode parameters for the Gigabit
  2324. * Ethernet Controller according the given parameters struct.
  2325. *
  2326. * INPUT:
  2327. * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
  2328. * ETH_WIN_PARAM *param Address decode parameter struct.
  2329. *
  2330. * OUTPUT:
  2331. * An access window is opened using the given access parameters.
  2332. *
  2333. * RETURN:
  2334. * None.
  2335. *
  2336. *******************************************************************************/
  2337. static void eth_set_access_control (ETH_PORT eth_port_num,
  2338. ETH_WIN_PARAM * param)
  2339. {
  2340. unsigned int access_prot_reg;
  2341. /* Set access control register */
  2342. access_prot_reg = MV_REG_READ (MV64460_ETH_ACCESS_PROTECTION_REG
  2343. (eth_port_num));
  2344. access_prot_reg &= (~(3 << (param->win * 2))); /* clear window permission */
  2345. access_prot_reg |= (param->access_ctrl << (param->win * 2));
  2346. MV_REG_WRITE (MV64460_ETH_ACCESS_PROTECTION_REG (eth_port_num),
  2347. access_prot_reg);
  2348. /* Set window Size reg (SR) */
  2349. MV_REG_WRITE ((MV64460_ETH_SIZE_REG_0 +
  2350. (ETH_SIZE_REG_GAP * param->win)),
  2351. (((param->size / 0x10000) - 1) << 16));
  2352. /* Set window Base address reg (BA) */
  2353. MV_REG_WRITE ((MV64460_ETH_BAR_0 + (ETH_BAR_GAP * param->win)),
  2354. (param->target | param->attributes | param->base_addr));
  2355. /* High address remap reg (HARR) */
  2356. if (param->win < 4)
  2357. MV_REG_WRITE ((MV64460_ETH_HIGH_ADDR_REMAP_REG_0 +
  2358. (ETH_HIGH_ADDR_REMAP_REG_GAP * param->win)),
  2359. param->high_addr);
  2360. /* Base address enable reg (BARER) */
  2361. if (param->enable == 1)
  2362. MV_RESET_REG_BITS (MV64460_ETH_BASE_ADDR_ENABLE_REG,
  2363. (1 << param->win));
  2364. else
  2365. MV_SET_REG_BITS (MV64460_ETH_BASE_ADDR_ENABLE_REG,
  2366. (1 << param->win));
  2367. }
  2368. /*******************************************************************************
  2369. * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
  2370. *
  2371. * DESCRIPTION:
  2372. * This function prepares a Rx chained list of descriptors and packet
  2373. * buffers in a form of a ring. The routine must be called after port
  2374. * initialization routine and before port start routine.
  2375. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  2376. * devices in the system (i.e. DRAM). This function uses the ethernet
  2377. * struct 'virtual to physical' routine (set by the user) to set the ring
  2378. * with physical addresses.
  2379. *
  2380. * INPUT:
  2381. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
  2382. * ETH_QUEUE rx_queue Number of Rx queue.
  2383. * int rx_desc_num Number of Rx descriptors
  2384. * int rx_buff_size Size of Rx buffer
  2385. * unsigned int rx_desc_base_addr Rx descriptors memory area base addr.
  2386. * unsigned int rx_buff_base_addr Rx buffer memory area base addr.
  2387. *
  2388. * OUTPUT:
  2389. * The routine updates the Ethernet port control struct with information
  2390. * regarding the Rx descriptors and buffers.
  2391. *
  2392. * RETURN:
  2393. * false if the given descriptors memory area is not aligned according to
  2394. * Ethernet SDMA specifications.
  2395. * true otherwise.
  2396. *
  2397. *******************************************************************************/
  2398. static bool ether_init_rx_desc_ring (ETH_PORT_INFO * p_eth_port_ctrl,
  2399. ETH_QUEUE rx_queue,
  2400. int rx_desc_num,
  2401. int rx_buff_size,
  2402. unsigned int rx_desc_base_addr,
  2403. unsigned int rx_buff_base_addr)
  2404. {
  2405. ETH_RX_DESC *p_rx_desc;
  2406. ETH_RX_DESC *p_rx_prev_desc; /* pointer to link with the last descriptor */
  2407. unsigned int buffer_addr;
  2408. int ix; /* a counter */
  2409. p_rx_desc = (ETH_RX_DESC *) rx_desc_base_addr;
  2410. p_rx_prev_desc = p_rx_desc;
  2411. buffer_addr = rx_buff_base_addr;
  2412. /* Rx desc Must be 4LW aligned (i.e. Descriptor_Address[3:0]=0000). */
  2413. if (rx_buff_base_addr & 0xF)
  2414. return false;
  2415. /* Rx buffers are limited to 64K bytes and Minimum size is 8 bytes */
  2416. if ((rx_buff_size < 8) || (rx_buff_size > RX_BUFFER_MAX_SIZE))
  2417. return false;
  2418. /* Rx buffers must be 64-bit aligned. */
  2419. if ((rx_buff_base_addr + rx_buff_size) & 0x7)
  2420. return false;
  2421. /* initialize the Rx descriptors ring */
  2422. for (ix = 0; ix < rx_desc_num; ix++) {
  2423. p_rx_desc->buf_size = rx_buff_size;
  2424. p_rx_desc->byte_cnt = 0x0000;
  2425. p_rx_desc->cmd_sts =
  2426. ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
  2427. p_rx_desc->next_desc_ptr =
  2428. ((unsigned int) p_rx_desc) + RX_DESC_ALIGNED_SIZE;
  2429. p_rx_desc->buf_ptr = buffer_addr;
  2430. p_rx_desc->return_info = 0x00000000;
  2431. D_CACHE_FLUSH_LINE (p_rx_desc, 0);
  2432. buffer_addr += rx_buff_size;
  2433. p_rx_prev_desc = p_rx_desc;
  2434. p_rx_desc = (ETH_RX_DESC *)
  2435. ((unsigned int) p_rx_desc + RX_DESC_ALIGNED_SIZE);
  2436. }
  2437. /* Closing Rx descriptors ring */
  2438. p_rx_prev_desc->next_desc_ptr = (rx_desc_base_addr);
  2439. D_CACHE_FLUSH_LINE (p_rx_prev_desc, 0);
  2440. /* Save Rx desc pointer to driver struct. */
  2441. CURR_RFD_SET ((ETH_RX_DESC *) rx_desc_base_addr, rx_queue);
  2442. USED_RFD_SET ((ETH_RX_DESC *) rx_desc_base_addr, rx_queue);
  2443. p_eth_port_ctrl->p_rx_desc_area_base[rx_queue] =
  2444. (ETH_RX_DESC *) rx_desc_base_addr;
  2445. p_eth_port_ctrl->rx_desc_area_size[rx_queue] =
  2446. rx_desc_num * RX_DESC_ALIGNED_SIZE;
  2447. p_eth_port_ctrl->port_rx_queue_command |= (1 << rx_queue);
  2448. return true;
  2449. }
  2450. /*******************************************************************************
  2451. * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
  2452. *
  2453. * DESCRIPTION:
  2454. * This function prepares a Tx chained list of descriptors and packet
  2455. * buffers in a form of a ring. The routine must be called after port
  2456. * initialization routine and before port start routine.
  2457. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  2458. * devices in the system (i.e. DRAM). This function uses the ethernet
  2459. * struct 'virtual to physical' routine (set by the user) to set the ring
  2460. * with physical addresses.
  2461. *
  2462. * INPUT:
  2463. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
  2464. * ETH_QUEUE tx_queue Number of Tx queue.
  2465. * int tx_desc_num Number of Tx descriptors
  2466. * int tx_buff_size Size of Tx buffer
  2467. * unsigned int tx_desc_base_addr Tx descriptors memory area base addr.
  2468. * unsigned int tx_buff_base_addr Tx buffer memory area base addr.
  2469. *
  2470. * OUTPUT:
  2471. * The routine updates the Ethernet port control struct with information
  2472. * regarding the Tx descriptors and buffers.
  2473. *
  2474. * RETURN:
  2475. * false if the given descriptors memory area is not aligned according to
  2476. * Ethernet SDMA specifications.
  2477. * true otherwise.
  2478. *
  2479. *******************************************************************************/
  2480. static bool ether_init_tx_desc_ring (ETH_PORT_INFO * p_eth_port_ctrl,
  2481. ETH_QUEUE tx_queue,
  2482. int tx_desc_num,
  2483. int tx_buff_size,
  2484. unsigned int tx_desc_base_addr,
  2485. unsigned int tx_buff_base_addr)
  2486. {
  2487. ETH_TX_DESC *p_tx_desc;
  2488. ETH_TX_DESC *p_tx_prev_desc;
  2489. unsigned int buffer_addr;
  2490. int ix; /* a counter */
  2491. /* save the first desc pointer to link with the last descriptor */
  2492. p_tx_desc = (ETH_TX_DESC *) tx_desc_base_addr;
  2493. p_tx_prev_desc = p_tx_desc;
  2494. buffer_addr = tx_buff_base_addr;
  2495. /* Tx desc Must be 4LW aligned (i.e. Descriptor_Address[3:0]=0000). */
  2496. if (tx_buff_base_addr & 0xF)
  2497. return false;
  2498. /* Tx buffers are limited to 64K bytes and Minimum size is 8 bytes */
  2499. if ((tx_buff_size > TX_BUFFER_MAX_SIZE)
  2500. || (tx_buff_size < TX_BUFFER_MIN_SIZE))
  2501. return false;
  2502. /* Initialize the Tx descriptors ring */
  2503. for (ix = 0; ix < tx_desc_num; ix++) {
  2504. p_tx_desc->byte_cnt = 0x0000;
  2505. p_tx_desc->l4i_chk = 0x0000;
  2506. p_tx_desc->cmd_sts = 0x00000000;
  2507. p_tx_desc->next_desc_ptr =
  2508. ((unsigned int) p_tx_desc) + TX_DESC_ALIGNED_SIZE;
  2509. p_tx_desc->buf_ptr = buffer_addr;
  2510. p_tx_desc->return_info = 0x00000000;
  2511. D_CACHE_FLUSH_LINE (p_tx_desc, 0);
  2512. buffer_addr += tx_buff_size;
  2513. p_tx_prev_desc = p_tx_desc;
  2514. p_tx_desc = (ETH_TX_DESC *)
  2515. ((unsigned int) p_tx_desc + TX_DESC_ALIGNED_SIZE);
  2516. }
  2517. /* Closing Tx descriptors ring */
  2518. p_tx_prev_desc->next_desc_ptr = tx_desc_base_addr;
  2519. D_CACHE_FLUSH_LINE (p_tx_prev_desc, 0);
  2520. /* Set Tx desc pointer in driver struct. */
  2521. CURR_TFD_SET ((ETH_TX_DESC *) tx_desc_base_addr, tx_queue);
  2522. USED_TFD_SET ((ETH_TX_DESC *) tx_desc_base_addr, tx_queue);
  2523. /* Init Tx ring base and size parameters */
  2524. p_eth_port_ctrl->p_tx_desc_area_base[tx_queue] =
  2525. (ETH_TX_DESC *) tx_desc_base_addr;
  2526. p_eth_port_ctrl->tx_desc_area_size[tx_queue] =
  2527. (tx_desc_num * TX_DESC_ALIGNED_SIZE);
  2528. /* Add the queue to the list of Tx queues of this port */
  2529. p_eth_port_ctrl->port_tx_queue_command |= (1 << tx_queue);
  2530. return true;
  2531. }
  2532. /*******************************************************************************
  2533. * eth_port_send - Send an Ethernet packet
  2534. *
  2535. * DESCRIPTION:
  2536. * This routine send a given packet described by p_pktinfo parameter. It
  2537. * supports transmitting of a packet spaned over multiple buffers. The
  2538. * routine updates 'curr' and 'first' indexes according to the packet
  2539. * segment passed to the routine. In case the packet segment is first,
  2540. * the 'first' index is update. In any case, the 'curr' index is updated.
  2541. * If the routine get into Tx resource error it assigns 'curr' index as
  2542. * 'first'. This way the function can abort Tx process of multiple
  2543. * descriptors per packet.
  2544. *
  2545. * INPUT:
  2546. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
  2547. * ETH_QUEUE tx_queue Number of Tx queue.
  2548. * PKT_INFO *p_pkt_info User packet buffer.
  2549. *
  2550. * OUTPUT:
  2551. * Tx ring 'curr' and 'first' indexes are updated.
  2552. *
  2553. * RETURN:
  2554. * ETH_QUEUE_FULL in case of Tx resource error.
  2555. * ETH_ERROR in case the routine can not access Tx desc ring.
  2556. * ETH_QUEUE_LAST_RESOURCE if the routine uses the last Tx resource.
  2557. * ETH_OK otherwise.
  2558. *
  2559. *******************************************************************************/
  2560. static ETH_FUNC_RET_STATUS eth_port_send (ETH_PORT_INFO * p_eth_port_ctrl,
  2561. ETH_QUEUE tx_queue,
  2562. PKT_INFO * p_pkt_info)
  2563. {
  2564. volatile ETH_TX_DESC *p_tx_desc_first;
  2565. volatile ETH_TX_DESC *p_tx_desc_curr;
  2566. volatile ETH_TX_DESC *p_tx_next_desc_curr;
  2567. volatile ETH_TX_DESC *p_tx_desc_used;
  2568. unsigned int command_status;
  2569. /* Do not process Tx ring in case of Tx ring resource error */
  2570. if (p_eth_port_ctrl->tx_resource_err[tx_queue] == true)
  2571. return ETH_QUEUE_FULL;
  2572. /* Get the Tx Desc ring indexes */
  2573. CURR_TFD_GET (p_tx_desc_curr, tx_queue);
  2574. USED_TFD_GET (p_tx_desc_used, tx_queue);
  2575. if (p_tx_desc_curr == NULL)
  2576. return ETH_ERROR;
  2577. /* The following parameters are used to save readings from memory */
  2578. p_tx_next_desc_curr = TX_NEXT_DESC_PTR (p_tx_desc_curr, tx_queue);
  2579. command_status = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC;
  2580. if (command_status & (ETH_TX_FIRST_DESC)) {
  2581. /* Update first desc */
  2582. FIRST_TFD_SET (p_tx_desc_curr, tx_queue);
  2583. p_tx_desc_first = p_tx_desc_curr;
  2584. } else {
  2585. FIRST_TFD_GET (p_tx_desc_first, tx_queue);
  2586. command_status |= ETH_BUFFER_OWNED_BY_DMA;
  2587. }
  2588. /* Buffers with a payload smaller than 8 bytes must be aligned to 64-bit */
  2589. /* boundary. We use the memory allocated for Tx descriptor. This memory */
  2590. /* located in TX_BUF_OFFSET_IN_DESC offset within the Tx descriptor. */
  2591. if (p_pkt_info->byte_cnt <= 8) {
  2592. printf ("You have failed in the < 8 bytes errata - fixme\n"); /* RABEEH - TBD */
  2593. return ETH_ERROR;
  2594. p_tx_desc_curr->buf_ptr =
  2595. (unsigned int) p_tx_desc_curr + TX_BUF_OFFSET_IN_DESC;
  2596. eth_b_copy (p_pkt_info->buf_ptr, p_tx_desc_curr->buf_ptr,
  2597. p_pkt_info->byte_cnt);
  2598. } else
  2599. p_tx_desc_curr->buf_ptr = p_pkt_info->buf_ptr;
  2600. p_tx_desc_curr->byte_cnt = p_pkt_info->byte_cnt;
  2601. p_tx_desc_curr->return_info = p_pkt_info->return_info;
  2602. if (p_pkt_info->cmd_sts & (ETH_TX_LAST_DESC)) {
  2603. /* Set last desc with DMA ownership and interrupt enable. */
  2604. p_tx_desc_curr->cmd_sts = command_status |
  2605. ETH_BUFFER_OWNED_BY_DMA | ETH_TX_ENABLE_INTERRUPT;
  2606. if (p_tx_desc_curr != p_tx_desc_first)
  2607. p_tx_desc_first->cmd_sts |= ETH_BUFFER_OWNED_BY_DMA;
  2608. /* Flush CPU pipe */
  2609. D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_curr, 0);
  2610. D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_first, 0);
  2611. CPU_PIPE_FLUSH;
  2612. /* Apply send command */
  2613. ETH_ENABLE_TX_QUEUE (tx_queue, p_eth_port_ctrl->port_num);
  2614. /* Finish Tx packet. Update first desc in case of Tx resource error */
  2615. p_tx_desc_first = p_tx_next_desc_curr;
  2616. FIRST_TFD_SET (p_tx_desc_first, tx_queue);
  2617. } else {
  2618. p_tx_desc_curr->cmd_sts = command_status;
  2619. D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_curr, 0);
  2620. }
  2621. /* Check for ring index overlap in the Tx desc ring */
  2622. if (p_tx_next_desc_curr == p_tx_desc_used) {
  2623. /* Update the current descriptor */
  2624. CURR_TFD_SET (p_tx_desc_first, tx_queue);
  2625. p_eth_port_ctrl->tx_resource_err[tx_queue] = true;
  2626. return ETH_QUEUE_LAST_RESOURCE;
  2627. } else {
  2628. /* Update the current descriptor */
  2629. CURR_TFD_SET (p_tx_next_desc_curr, tx_queue);
  2630. return ETH_OK;
  2631. }
  2632. }
  2633. /*******************************************************************************
  2634. * eth_tx_return_desc - Free all used Tx descriptors
  2635. *
  2636. * DESCRIPTION:
  2637. * This routine returns the transmitted packet information to the caller.
  2638. * It uses the 'first' index to support Tx desc return in case a transmit
  2639. * of a packet spanned over multiple buffer still in process.
  2640. * In case the Tx queue was in "resource error" condition, where there are
  2641. * no available Tx resources, the function resets the resource error flag.
  2642. *
  2643. * INPUT:
  2644. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
  2645. * ETH_QUEUE tx_queue Number of Tx queue.
  2646. * PKT_INFO *p_pkt_info User packet buffer.
  2647. *
  2648. * OUTPUT:
  2649. * Tx ring 'first' and 'used' indexes are updated.
  2650. *
  2651. * RETURN:
  2652. * ETH_ERROR in case the routine can not access Tx desc ring.
  2653. * ETH_RETRY in case there is transmission in process.
  2654. * ETH_END_OF_JOB if the routine has nothing to release.
  2655. * ETH_OK otherwise.
  2656. *
  2657. *******************************************************************************/
  2658. static ETH_FUNC_RET_STATUS eth_tx_return_desc (ETH_PORT_INFO *
  2659. p_eth_port_ctrl,
  2660. ETH_QUEUE tx_queue,
  2661. PKT_INFO * p_pkt_info)
  2662. {
  2663. volatile ETH_TX_DESC *p_tx_desc_used = NULL;
  2664. volatile ETH_TX_DESC *p_tx_desc_first = NULL;
  2665. unsigned int command_status;
  2666. /* Get the Tx Desc ring indexes */
  2667. USED_TFD_GET (p_tx_desc_used, tx_queue);
  2668. FIRST_TFD_GET (p_tx_desc_first, tx_queue);
  2669. /* Sanity check */
  2670. if (p_tx_desc_used == NULL)
  2671. return ETH_ERROR;
  2672. command_status = p_tx_desc_used->cmd_sts;
  2673. /* Still transmitting... */
  2674. if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
  2675. D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_used, 0);
  2676. return ETH_RETRY;
  2677. }
  2678. /* Stop release. About to overlap the current available Tx descriptor */
  2679. if ((p_tx_desc_used == p_tx_desc_first) &&
  2680. (p_eth_port_ctrl->tx_resource_err[tx_queue] == false)) {
  2681. D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_used, 0);
  2682. return ETH_END_OF_JOB;
  2683. }
  2684. /* Pass the packet information to the caller */
  2685. p_pkt_info->cmd_sts = command_status;
  2686. p_pkt_info->return_info = p_tx_desc_used->return_info;
  2687. p_tx_desc_used->return_info = 0;
  2688. /* Update the next descriptor to release. */
  2689. USED_TFD_SET (TX_NEXT_DESC_PTR (p_tx_desc_used, tx_queue), tx_queue);
  2690. /* Any Tx return cancels the Tx resource error status */
  2691. if (p_eth_port_ctrl->tx_resource_err[tx_queue] == true)
  2692. p_eth_port_ctrl->tx_resource_err[tx_queue] = false;
  2693. D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_used, 0);
  2694. return ETH_OK;
  2695. }
  2696. /*******************************************************************************
  2697. * eth_port_receive - Get received information from Rx ring.
  2698. *
  2699. * DESCRIPTION:
  2700. * This routine returns the received data to the caller. There is no
  2701. * data copying during routine operation. All information is returned
  2702. * using pointer to packet information struct passed from the caller.
  2703. * If the routine exhausts Rx ring resources then the resource error flag
  2704. * is set.
  2705. *
  2706. * INPUT:
  2707. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
  2708. * ETH_QUEUE rx_queue Number of Rx queue.
  2709. * PKT_INFO *p_pkt_info User packet buffer.
  2710. *
  2711. * OUTPUT:
  2712. * Rx ring current and used indexes are updated.
  2713. *
  2714. * RETURN:
  2715. * ETH_ERROR in case the routine can not access Rx desc ring.
  2716. * ETH_QUEUE_FULL if Rx ring resources are exhausted.
  2717. * ETH_END_OF_JOB if there is no received data.
  2718. * ETH_OK otherwise.
  2719. *
  2720. *******************************************************************************/
  2721. static ETH_FUNC_RET_STATUS eth_port_receive (ETH_PORT_INFO * p_eth_port_ctrl,
  2722. ETH_QUEUE rx_queue,
  2723. PKT_INFO * p_pkt_info)
  2724. {
  2725. volatile ETH_RX_DESC *p_rx_curr_desc;
  2726. volatile ETH_RX_DESC *p_rx_next_curr_desc;
  2727. volatile ETH_RX_DESC *p_rx_used_desc;
  2728. unsigned int command_status;
  2729. /* Do not process Rx ring in case of Rx ring resource error */
  2730. if (p_eth_port_ctrl->rx_resource_err[rx_queue] == true) {
  2731. printf ("\nRx Queue is full ...\n");
  2732. return ETH_QUEUE_FULL;
  2733. }
  2734. /* Get the Rx Desc ring 'curr and 'used' indexes */
  2735. CURR_RFD_GET (p_rx_curr_desc, rx_queue);
  2736. USED_RFD_GET (p_rx_used_desc, rx_queue);
  2737. /* Sanity check */
  2738. if (p_rx_curr_desc == NULL)
  2739. return ETH_ERROR;
  2740. /* The following parameters are used to save readings from memory */
  2741. p_rx_next_curr_desc = RX_NEXT_DESC_PTR (p_rx_curr_desc, rx_queue);
  2742. command_status = p_rx_curr_desc->cmd_sts;
  2743. /* Nothing to receive... */
  2744. if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
  2745. /* DP(printf("Rx: command_status: %08x\n", command_status)); */
  2746. D_CACHE_FLUSH_LINE ((unsigned int) p_rx_curr_desc, 0);
  2747. /* DP(printf("\nETH_END_OF_JOB ...\n"));*/
  2748. return ETH_END_OF_JOB;
  2749. }
  2750. p_pkt_info->byte_cnt = (p_rx_curr_desc->byte_cnt) - RX_BUF_OFFSET;
  2751. p_pkt_info->cmd_sts = command_status;
  2752. p_pkt_info->buf_ptr = (p_rx_curr_desc->buf_ptr) + RX_BUF_OFFSET;
  2753. p_pkt_info->return_info = p_rx_curr_desc->return_info;
  2754. p_pkt_info->l4i_chk = p_rx_curr_desc->buf_size; /* IP fragment indicator */
  2755. /* Clean the return info field to indicate that the packet has been */
  2756. /* moved to the upper layers */
  2757. p_rx_curr_desc->return_info = 0;
  2758. /* Update 'curr' in data structure */
  2759. CURR_RFD_SET (p_rx_next_curr_desc, rx_queue);
  2760. /* Rx descriptors resource exhausted. Set the Rx ring resource error flag */
  2761. if (p_rx_next_curr_desc == p_rx_used_desc)
  2762. p_eth_port_ctrl->rx_resource_err[rx_queue] = true;
  2763. D_CACHE_FLUSH_LINE ((unsigned int) p_rx_curr_desc, 0);
  2764. CPU_PIPE_FLUSH;
  2765. return ETH_OK;
  2766. }
  2767. /*******************************************************************************
  2768. * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
  2769. *
  2770. * DESCRIPTION:
  2771. * This routine returns a Rx buffer back to the Rx ring. It retrieves the
  2772. * next 'used' descriptor and attached the returned buffer to it.
  2773. * In case the Rx ring was in "resource error" condition, where there are
  2774. * no available Rx resources, the function resets the resource error flag.
  2775. *
  2776. * INPUT:
  2777. * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
  2778. * ETH_QUEUE rx_queue Number of Rx queue.
  2779. * PKT_INFO *p_pkt_info Information on the returned buffer.
  2780. *
  2781. * OUTPUT:
  2782. * New available Rx resource in Rx descriptor ring.
  2783. *
  2784. * RETURN:
  2785. * ETH_ERROR in case the routine can not access Rx desc ring.
  2786. * ETH_OK otherwise.
  2787. *
  2788. *******************************************************************************/
  2789. static ETH_FUNC_RET_STATUS eth_rx_return_buff (ETH_PORT_INFO *
  2790. p_eth_port_ctrl,
  2791. ETH_QUEUE rx_queue,
  2792. PKT_INFO * p_pkt_info)
  2793. {
  2794. volatile ETH_RX_DESC *p_used_rx_desc; /* Where to return Rx resource */
  2795. /* Get 'used' Rx descriptor */
  2796. USED_RFD_GET (p_used_rx_desc, rx_queue);
  2797. /* Sanity check */
  2798. if (p_used_rx_desc == NULL)
  2799. return ETH_ERROR;
  2800. p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
  2801. p_used_rx_desc->return_info = p_pkt_info->return_info;
  2802. p_used_rx_desc->byte_cnt = p_pkt_info->byte_cnt;
  2803. p_used_rx_desc->buf_size = MV64460_RX_BUFFER_SIZE; /* Reset Buffer size */
  2804. /* Flush the write pipe */
  2805. CPU_PIPE_FLUSH;
  2806. /* Return the descriptor to DMA ownership */
  2807. p_used_rx_desc->cmd_sts =
  2808. ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
  2809. /* Flush descriptor and CPU pipe */
  2810. D_CACHE_FLUSH_LINE ((unsigned int) p_used_rx_desc, 0);
  2811. CPU_PIPE_FLUSH;
  2812. /* Move the used descriptor pointer to the next descriptor */
  2813. USED_RFD_SET (RX_NEXT_DESC_PTR (p_used_rx_desc, rx_queue), rx_queue);
  2814. /* Any Rx return cancels the Rx resource error status */
  2815. if (p_eth_port_ctrl->rx_resource_err[rx_queue] == true)
  2816. p_eth_port_ctrl->rx_resource_err[rx_queue] = false;
  2817. return ETH_OK;
  2818. }
  2819. /*******************************************************************************
  2820. * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
  2821. *
  2822. * DESCRIPTION:
  2823. * This routine sets the RX coalescing interrupt mechanism parameter.
  2824. * This parameter is a timeout counter, that counts in 64 t_clk
  2825. * chunks ; that when timeout event occurs a maskable interrupt
  2826. * occurs.
  2827. * The parameter is calculated using the tClk of the MV-643xx chip
  2828. * , and the required delay of the interrupt in usec.
  2829. *
  2830. * INPUT:
  2831. * ETH_PORT eth_port_num Ethernet port number
  2832. * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
  2833. * unsigned int delay Delay in usec
  2834. *
  2835. * OUTPUT:
  2836. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  2837. *
  2838. * RETURN:
  2839. * The interrupt coalescing value set in the gigE port.
  2840. *
  2841. *******************************************************************************/
  2842. #if 0 /* FIXME */
  2843. static unsigned int eth_port_set_rx_coal (ETH_PORT eth_port_num,
  2844. unsigned int t_clk,
  2845. unsigned int delay)
  2846. {
  2847. unsigned int coal;
  2848. coal = ((t_clk / 1000000) * delay) / 64;
  2849. /* Set RX Coalescing mechanism */
  2850. MV_REG_WRITE (MV64460_ETH_SDMA_CONFIG_REG (eth_port_num),
  2851. ((coal & 0x3fff) << 8) |
  2852. (MV_REG_READ
  2853. (MV64460_ETH_SDMA_CONFIG_REG (eth_port_num))
  2854. & 0xffc000ff));
  2855. return coal;
  2856. }
  2857. #endif
  2858. /*******************************************************************************
  2859. * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
  2860. *
  2861. * DESCRIPTION:
  2862. * This routine sets the TX coalescing interrupt mechanism parameter.
  2863. * This parameter is a timeout counter, that counts in 64 t_clk
  2864. * chunks ; that when timeout event occurs a maskable interrupt
  2865. * occurs.
  2866. * The parameter is calculated using the t_cLK frequency of the
  2867. * MV-643xx chip and the required delay in the interrupt in uSec
  2868. *
  2869. * INPUT:
  2870. * ETH_PORT eth_port_num Ethernet port number
  2871. * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
  2872. * unsigned int delay Delay in uSeconds
  2873. *
  2874. * OUTPUT:
  2875. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  2876. *
  2877. * RETURN:
  2878. * The interrupt coalescing value set in the gigE port.
  2879. *
  2880. *******************************************************************************/
  2881. #if 0 /* FIXME */
  2882. static unsigned int eth_port_set_tx_coal (ETH_PORT eth_port_num,
  2883. unsigned int t_clk,
  2884. unsigned int delay)
  2885. {
  2886. unsigned int coal;
  2887. coal = ((t_clk / 1000000) * delay) / 64;
  2888. /* Set TX Coalescing mechanism */
  2889. MV_REG_WRITE (MV64460_ETH_TX_FIFO_URGENT_THRESHOLD_REG (eth_port_num),
  2890. coal << 4);
  2891. return coal;
  2892. }
  2893. #endif
  2894. /*******************************************************************************
  2895. * eth_b_copy - Copy bytes from source to destination
  2896. *
  2897. * DESCRIPTION:
  2898. * This function supports the eight bytes limitation on Tx buffer size.
  2899. * The routine will zero eight bytes starting from the destination address
  2900. * followed by copying bytes from the source address to the destination.
  2901. *
  2902. * INPUT:
  2903. * unsigned int src_addr 32 bit source address.
  2904. * unsigned int dst_addr 32 bit destination address.
  2905. * int byte_count Number of bytes to copy.
  2906. *
  2907. * OUTPUT:
  2908. * See description.
  2909. *
  2910. * RETURN:
  2911. * None.
  2912. *
  2913. *******************************************************************************/
  2914. static void eth_b_copy (unsigned int src_addr, unsigned int dst_addr,
  2915. int byte_count)
  2916. {
  2917. /* Zero the dst_addr area */
  2918. *(unsigned int *) dst_addr = 0x0;
  2919. while (byte_count != 0) {
  2920. *(char *) dst_addr = *(char *) src_addr;
  2921. dst_addr++;
  2922. src_addr++;
  2923. byte_count--;
  2924. }
  2925. }