mv_eth.c 107 KB

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