sunhme.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404
  1. /* sunhme.c: Sparc HME/BigMac 10/100baseT half/full duplex auto switching,
  2. * auto carrier detecting ethernet driver. Also known as the
  3. * "Happy Meal Ethernet" found on SunSwift SBUS cards.
  4. *
  5. * Copyright (C) 1996, 1998, 1999, 2002, 2003,
  6. 2006 David S. Miller (davem@davemloft.net)
  7. *
  8. * Changes :
  9. * 2000/11/11 Willy Tarreau <willy AT meta-x.org>
  10. * - port to non-sparc architectures. Tested only on x86 and
  11. * only currently works with QFE PCI cards.
  12. * - ability to specify the MAC address at module load time by passing this
  13. * argument : macaddr=0x00,0x10,0x20,0x30,0x40,0x50
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioport.h>
  21. #include <linux/in.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/mii.h>
  28. #include <linux/crc32.h>
  29. #include <linux/random.h>
  30. #include <linux/errno.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/mm.h>
  35. #include <linux/bitops.h>
  36. #include <asm/system.h>
  37. #include <asm/io.h>
  38. #include <asm/dma.h>
  39. #include <asm/byteorder.h>
  40. #ifdef CONFIG_SPARC
  41. #include <asm/idprom.h>
  42. #include <asm/sbus.h>
  43. #include <asm/openprom.h>
  44. #include <asm/oplib.h>
  45. #include <asm/prom.h>
  46. #include <asm/auxio.h>
  47. #endif
  48. #include <asm/uaccess.h>
  49. #include <asm/pgtable.h>
  50. #include <asm/irq.h>
  51. #ifdef CONFIG_PCI
  52. #include <linux/pci.h>
  53. #endif
  54. #include "sunhme.h"
  55. #define DRV_NAME "sunhme"
  56. #define DRV_VERSION "3.00"
  57. #define DRV_RELDATE "June 23, 2006"
  58. #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
  59. static char version[] =
  60. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  61. MODULE_VERSION(DRV_VERSION);
  62. MODULE_AUTHOR(DRV_AUTHOR);
  63. MODULE_DESCRIPTION("Sun HappyMealEthernet(HME) 10/100baseT ethernet driver");
  64. MODULE_LICENSE("GPL");
  65. static int macaddr[6];
  66. /* accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
  67. module_param_array(macaddr, int, NULL, 0);
  68. MODULE_PARM_DESC(macaddr, "Happy Meal MAC address to set");
  69. #ifdef CONFIG_SBUS
  70. static struct quattro *qfe_sbus_list;
  71. #endif
  72. #ifdef CONFIG_PCI
  73. static struct quattro *qfe_pci_list;
  74. #endif
  75. #undef HMEDEBUG
  76. #undef SXDEBUG
  77. #undef RXDEBUG
  78. #undef TXDEBUG
  79. #undef TXLOGGING
  80. #ifdef TXLOGGING
  81. struct hme_tx_logent {
  82. unsigned int tstamp;
  83. int tx_new, tx_old;
  84. unsigned int action;
  85. #define TXLOG_ACTION_IRQ 0x01
  86. #define TXLOG_ACTION_TXMIT 0x02
  87. #define TXLOG_ACTION_TBUSY 0x04
  88. #define TXLOG_ACTION_NBUFS 0x08
  89. unsigned int status;
  90. };
  91. #define TX_LOG_LEN 128
  92. static struct hme_tx_logent tx_log[TX_LOG_LEN];
  93. static int txlog_cur_entry;
  94. static __inline__ void tx_add_log(struct happy_meal *hp, unsigned int a, unsigned int s)
  95. {
  96. struct hme_tx_logent *tlp;
  97. unsigned long flags;
  98. local_irq_save(flags);
  99. tlp = &tx_log[txlog_cur_entry];
  100. tlp->tstamp = (unsigned int)jiffies;
  101. tlp->tx_new = hp->tx_new;
  102. tlp->tx_old = hp->tx_old;
  103. tlp->action = a;
  104. tlp->status = s;
  105. txlog_cur_entry = (txlog_cur_entry + 1) & (TX_LOG_LEN - 1);
  106. local_irq_restore(flags);
  107. }
  108. static __inline__ void tx_dump_log(void)
  109. {
  110. int i, this;
  111. this = txlog_cur_entry;
  112. for (i = 0; i < TX_LOG_LEN; i++) {
  113. printk("TXLOG[%d]: j[%08x] tx[N(%d)O(%d)] action[%08x] stat[%08x]\n", i,
  114. tx_log[this].tstamp,
  115. tx_log[this].tx_new, tx_log[this].tx_old,
  116. tx_log[this].action, tx_log[this].status);
  117. this = (this + 1) & (TX_LOG_LEN - 1);
  118. }
  119. }
  120. static __inline__ void tx_dump_ring(struct happy_meal *hp)
  121. {
  122. struct hmeal_init_block *hb = hp->happy_block;
  123. struct happy_meal_txd *tp = &hb->happy_meal_txd[0];
  124. int i;
  125. for (i = 0; i < TX_RING_SIZE; i+=4) {
  126. printk("TXD[%d..%d]: [%08x:%08x] [%08x:%08x] [%08x:%08x] [%08x:%08x]\n",
  127. i, i + 4,
  128. le32_to_cpu(tp[i].tx_flags), le32_to_cpu(tp[i].tx_addr),
  129. le32_to_cpu(tp[i + 1].tx_flags), le32_to_cpu(tp[i + 1].tx_addr),
  130. le32_to_cpu(tp[i + 2].tx_flags), le32_to_cpu(tp[i + 2].tx_addr),
  131. le32_to_cpu(tp[i + 3].tx_flags), le32_to_cpu(tp[i + 3].tx_addr));
  132. }
  133. }
  134. #else
  135. #define tx_add_log(hp, a, s) do { } while(0)
  136. #define tx_dump_log() do { } while(0)
  137. #define tx_dump_ring(hp) do { } while(0)
  138. #endif
  139. #ifdef HMEDEBUG
  140. #define HMD(x) printk x
  141. #else
  142. #define HMD(x)
  143. #endif
  144. /* #define AUTO_SWITCH_DEBUG */
  145. #ifdef AUTO_SWITCH_DEBUG
  146. #define ASD(x) printk x
  147. #else
  148. #define ASD(x)
  149. #endif
  150. #define DEFAULT_IPG0 16 /* For lance-mode only */
  151. #define DEFAULT_IPG1 8 /* For all modes */
  152. #define DEFAULT_IPG2 4 /* For all modes */
  153. #define DEFAULT_JAMSIZE 4 /* Toe jam */
  154. /* NOTE: In the descriptor writes one _must_ write the address
  155. * member _first_. The card must not be allowed to see
  156. * the updated descriptor flags until the address is
  157. * correct. I've added a write memory barrier between
  158. * the two stores so that I can sleep well at night... -DaveM
  159. */
  160. #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
  161. static void sbus_hme_write32(void __iomem *reg, u32 val)
  162. {
  163. sbus_writel(val, reg);
  164. }
  165. static u32 sbus_hme_read32(void __iomem *reg)
  166. {
  167. return sbus_readl(reg);
  168. }
  169. static void sbus_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
  170. {
  171. rxd->rx_addr = (__force hme32)addr;
  172. wmb();
  173. rxd->rx_flags = (__force hme32)flags;
  174. }
  175. static void sbus_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
  176. {
  177. txd->tx_addr = (__force hme32)addr;
  178. wmb();
  179. txd->tx_flags = (__force hme32)flags;
  180. }
  181. static u32 sbus_hme_read_desc32(hme32 *p)
  182. {
  183. return (__force u32)*p;
  184. }
  185. static void pci_hme_write32(void __iomem *reg, u32 val)
  186. {
  187. writel(val, reg);
  188. }
  189. static u32 pci_hme_read32(void __iomem *reg)
  190. {
  191. return readl(reg);
  192. }
  193. static void pci_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
  194. {
  195. rxd->rx_addr = (__force hme32)cpu_to_le32(addr);
  196. wmb();
  197. rxd->rx_flags = (__force hme32)cpu_to_le32(flags);
  198. }
  199. static void pci_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
  200. {
  201. txd->tx_addr = (__force hme32)cpu_to_le32(addr);
  202. wmb();
  203. txd->tx_flags = (__force hme32)cpu_to_le32(flags);
  204. }
  205. static u32 pci_hme_read_desc32(hme32 *p)
  206. {
  207. return le32_to_cpup((__le32 *)p);
  208. }
  209. #define hme_write32(__hp, __reg, __val) \
  210. ((__hp)->write32((__reg), (__val)))
  211. #define hme_read32(__hp, __reg) \
  212. ((__hp)->read32(__reg))
  213. #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
  214. ((__hp)->write_rxd((__rxd), (__flags), (__addr)))
  215. #define hme_write_txd(__hp, __txd, __flags, __addr) \
  216. ((__hp)->write_txd((__txd), (__flags), (__addr)))
  217. #define hme_read_desc32(__hp, __p) \
  218. ((__hp)->read_desc32(__p))
  219. #define hme_dma_map(__hp, __ptr, __size, __dir) \
  220. ((__hp)->dma_map((__hp)->happy_dev, (__ptr), (__size), (__dir)))
  221. #define hme_dma_unmap(__hp, __addr, __size, __dir) \
  222. ((__hp)->dma_unmap((__hp)->happy_dev, (__addr), (__size), (__dir)))
  223. #define hme_dma_sync_for_cpu(__hp, __addr, __size, __dir) \
  224. ((__hp)->dma_sync_for_cpu((__hp)->happy_dev, (__addr), (__size), (__dir)))
  225. #define hme_dma_sync_for_device(__hp, __addr, __size, __dir) \
  226. ((__hp)->dma_sync_for_device((__hp)->happy_dev, (__addr), (__size), (__dir)))
  227. #else
  228. #ifdef CONFIG_SBUS
  229. /* SBUS only compilation */
  230. #define hme_write32(__hp, __reg, __val) \
  231. sbus_writel((__val), (__reg))
  232. #define hme_read32(__hp, __reg) \
  233. sbus_readl(__reg)
  234. #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
  235. do { (__rxd)->rx_addr = (__force hme32)(u32)(__addr); \
  236. wmb(); \
  237. (__rxd)->rx_flags = (__force hme32)(u32)(__flags); \
  238. } while(0)
  239. #define hme_write_txd(__hp, __txd, __flags, __addr) \
  240. do { (__txd)->tx_addr = (__force hme32)(u32)(__addr); \
  241. wmb(); \
  242. (__txd)->tx_flags = (__force hme32)(u32)(__flags); \
  243. } while(0)
  244. #define hme_read_desc32(__hp, __p) ((__force u32)(hme32)*(__p))
  245. #define hme_dma_map(__hp, __ptr, __size, __dir) \
  246. sbus_map_single((__hp)->happy_dev, (__ptr), (__size), (__dir))
  247. #define hme_dma_unmap(__hp, __addr, __size, __dir) \
  248. sbus_unmap_single((__hp)->happy_dev, (__addr), (__size), (__dir))
  249. #define hme_dma_sync_for_cpu(__hp, __addr, __size, __dir) \
  250. sbus_dma_sync_single_for_cpu((__hp)->happy_dev, (__addr), (__size), (__dir))
  251. #define hme_dma_sync_for_device(__hp, __addr, __size, __dir) \
  252. sbus_dma_sync_single_for_device((__hp)->happy_dev, (__addr), (__size), (__dir))
  253. #else
  254. /* PCI only compilation */
  255. #define hme_write32(__hp, __reg, __val) \
  256. writel((__val), (__reg))
  257. #define hme_read32(__hp, __reg) \
  258. readl(__reg)
  259. #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
  260. do { (__rxd)->rx_addr = (__force hme32)cpu_to_le32(__addr); \
  261. wmb(); \
  262. (__rxd)->rx_flags = (__force hme32)cpu_to_le32(__flags); \
  263. } while(0)
  264. #define hme_write_txd(__hp, __txd, __flags, __addr) \
  265. do { (__txd)->tx_addr = (__force hme32)cpu_to_le32(__addr); \
  266. wmb(); \
  267. (__txd)->tx_flags = (__force hme32)cpu_to_le32(__flags); \
  268. } while(0)
  269. static inline u32 hme_read_desc32(struct happy_meal *hp, hme32 *p)
  270. {
  271. return le32_to_cpup((__le32 *)p);
  272. }
  273. #define hme_dma_map(__hp, __ptr, __size, __dir) \
  274. pci_map_single((__hp)->happy_dev, (__ptr), (__size), (__dir))
  275. #define hme_dma_unmap(__hp, __addr, __size, __dir) \
  276. pci_unmap_single((__hp)->happy_dev, (__addr), (__size), (__dir))
  277. #define hme_dma_sync_for_cpu(__hp, __addr, __size, __dir) \
  278. pci_dma_sync_single_for_cpu((__hp)->happy_dev, (__addr), (__size), (__dir))
  279. #define hme_dma_sync_for_device(__hp, __addr, __size, __dir) \
  280. pci_dma_sync_single_for_device((__hp)->happy_dev, (__addr), (__size), (__dir))
  281. #endif
  282. #endif
  283. #ifdef SBUS_DMA_BIDIRECTIONAL
  284. # define DMA_BIDIRECTIONAL SBUS_DMA_BIDIRECTIONAL
  285. #else
  286. # define DMA_BIDIRECTIONAL 0
  287. #endif
  288. #ifdef SBUS_DMA_FROMDEVICE
  289. # define DMA_FROMDEVICE SBUS_DMA_FROMDEVICE
  290. #else
  291. # define DMA_TODEVICE 1
  292. #endif
  293. #ifdef SBUS_DMA_TODEVICE
  294. # define DMA_TODEVICE SBUS_DMA_TODEVICE
  295. #else
  296. # define DMA_FROMDEVICE 2
  297. #endif
  298. /* Oh yes, the MIF BitBang is mighty fun to program. BitBucket is more like it. */
  299. static void BB_PUT_BIT(struct happy_meal *hp, void __iomem *tregs, int bit)
  300. {
  301. hme_write32(hp, tregs + TCVR_BBDATA, bit);
  302. hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
  303. hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
  304. }
  305. #if 0
  306. static u32 BB_GET_BIT(struct happy_meal *hp, void __iomem *tregs, int internal)
  307. {
  308. u32 ret;
  309. hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
  310. hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
  311. ret = hme_read32(hp, tregs + TCVR_CFG);
  312. if (internal)
  313. ret &= TCV_CFG_MDIO0;
  314. else
  315. ret &= TCV_CFG_MDIO1;
  316. return ret;
  317. }
  318. #endif
  319. static u32 BB_GET_BIT2(struct happy_meal *hp, void __iomem *tregs, int internal)
  320. {
  321. u32 retval;
  322. hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
  323. udelay(1);
  324. retval = hme_read32(hp, tregs + TCVR_CFG);
  325. if (internal)
  326. retval &= TCV_CFG_MDIO0;
  327. else
  328. retval &= TCV_CFG_MDIO1;
  329. hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
  330. return retval;
  331. }
  332. #define TCVR_FAILURE 0x80000000 /* Impossible MIF read value */
  333. static int happy_meal_bb_read(struct happy_meal *hp,
  334. void __iomem *tregs, int reg)
  335. {
  336. u32 tmp;
  337. int retval = 0;
  338. int i;
  339. ASD(("happy_meal_bb_read: reg=%d ", reg));
  340. /* Enable the MIF BitBang outputs. */
  341. hme_write32(hp, tregs + TCVR_BBOENAB, 1);
  342. /* Force BitBang into the idle state. */
  343. for (i = 0; i < 32; i++)
  344. BB_PUT_BIT(hp, tregs, 1);
  345. /* Give it the read sequence. */
  346. BB_PUT_BIT(hp, tregs, 0);
  347. BB_PUT_BIT(hp, tregs, 1);
  348. BB_PUT_BIT(hp, tregs, 1);
  349. BB_PUT_BIT(hp, tregs, 0);
  350. /* Give it the PHY address. */
  351. tmp = hp->paddr & 0xff;
  352. for (i = 4; i >= 0; i--)
  353. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  354. /* Tell it what register we want to read. */
  355. tmp = (reg & 0xff);
  356. for (i = 4; i >= 0; i--)
  357. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  358. /* Close down the MIF BitBang outputs. */
  359. hme_write32(hp, tregs + TCVR_BBOENAB, 0);
  360. /* Now read in the value. */
  361. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  362. for (i = 15; i >= 0; i--)
  363. retval |= BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  364. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  365. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  366. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  367. ASD(("value=%x\n", retval));
  368. return retval;
  369. }
  370. static void happy_meal_bb_write(struct happy_meal *hp,
  371. void __iomem *tregs, int reg,
  372. unsigned short value)
  373. {
  374. u32 tmp;
  375. int i;
  376. ASD(("happy_meal_bb_write: reg=%d value=%x\n", reg, value));
  377. /* Enable the MIF BitBang outputs. */
  378. hme_write32(hp, tregs + TCVR_BBOENAB, 1);
  379. /* Force BitBang into the idle state. */
  380. for (i = 0; i < 32; i++)
  381. BB_PUT_BIT(hp, tregs, 1);
  382. /* Give it write sequence. */
  383. BB_PUT_BIT(hp, tregs, 0);
  384. BB_PUT_BIT(hp, tregs, 1);
  385. BB_PUT_BIT(hp, tregs, 0);
  386. BB_PUT_BIT(hp, tregs, 1);
  387. /* Give it the PHY address. */
  388. tmp = (hp->paddr & 0xff);
  389. for (i = 4; i >= 0; i--)
  390. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  391. /* Tell it what register we will be writing. */
  392. tmp = (reg & 0xff);
  393. for (i = 4; i >= 0; i--)
  394. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  395. /* Tell it to become ready for the bits. */
  396. BB_PUT_BIT(hp, tregs, 1);
  397. BB_PUT_BIT(hp, tregs, 0);
  398. for (i = 15; i >= 0; i--)
  399. BB_PUT_BIT(hp, tregs, ((value >> i) & 1));
  400. /* Close down the MIF BitBang outputs. */
  401. hme_write32(hp, tregs + TCVR_BBOENAB, 0);
  402. }
  403. #define TCVR_READ_TRIES 16
  404. static int happy_meal_tcvr_read(struct happy_meal *hp,
  405. void __iomem *tregs, int reg)
  406. {
  407. int tries = TCVR_READ_TRIES;
  408. int retval;
  409. ASD(("happy_meal_tcvr_read: reg=0x%02x ", reg));
  410. if (hp->tcvr_type == none) {
  411. ASD(("no transceiver, value=TCVR_FAILURE\n"));
  412. return TCVR_FAILURE;
  413. }
  414. if (!(hp->happy_flags & HFLAG_FENABLE)) {
  415. ASD(("doing bit bang\n"));
  416. return happy_meal_bb_read(hp, tregs, reg);
  417. }
  418. hme_write32(hp, tregs + TCVR_FRAME,
  419. (FRAME_READ | (hp->paddr << 23) | ((reg & 0xff) << 18)));
  420. while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
  421. udelay(20);
  422. if (!tries) {
  423. printk(KERN_ERR "happy meal: Aieee, transceiver MIF read bolixed\n");
  424. return TCVR_FAILURE;
  425. }
  426. retval = hme_read32(hp, tregs + TCVR_FRAME) & 0xffff;
  427. ASD(("value=%04x\n", retval));
  428. return retval;
  429. }
  430. #define TCVR_WRITE_TRIES 16
  431. static void happy_meal_tcvr_write(struct happy_meal *hp,
  432. void __iomem *tregs, int reg,
  433. unsigned short value)
  434. {
  435. int tries = TCVR_WRITE_TRIES;
  436. ASD(("happy_meal_tcvr_write: reg=0x%02x value=%04x\n", reg, value));
  437. /* Welcome to Sun Microsystems, can I take your order please? */
  438. if (!(hp->happy_flags & HFLAG_FENABLE)) {
  439. happy_meal_bb_write(hp, tregs, reg, value);
  440. return;
  441. }
  442. /* Would you like fries with that? */
  443. hme_write32(hp, tregs + TCVR_FRAME,
  444. (FRAME_WRITE | (hp->paddr << 23) |
  445. ((reg & 0xff) << 18) | (value & 0xffff)));
  446. while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
  447. udelay(20);
  448. /* Anything else? */
  449. if (!tries)
  450. printk(KERN_ERR "happy meal: Aieee, transceiver MIF write bolixed\n");
  451. /* Fifty-two cents is your change, have a nice day. */
  452. }
  453. /* Auto negotiation. The scheme is very simple. We have a timer routine
  454. * that keeps watching the auto negotiation process as it progresses.
  455. * The DP83840 is first told to start doing it's thing, we set up the time
  456. * and place the timer state machine in it's initial state.
  457. *
  458. * Here the timer peeks at the DP83840 status registers at each click to see
  459. * if the auto negotiation has completed, we assume here that the DP83840 PHY
  460. * will time out at some point and just tell us what (didn't) happen. For
  461. * complete coverage we only allow so many of the ticks at this level to run,
  462. * when this has expired we print a warning message and try another strategy.
  463. * This "other" strategy is to force the interface into various speed/duplex
  464. * configurations and we stop when we see a link-up condition before the
  465. * maximum number of "peek" ticks have occurred.
  466. *
  467. * Once a valid link status has been detected we configure the BigMAC and
  468. * the rest of the Happy Meal to speak the most efficient protocol we could
  469. * get a clean link for. The priority for link configurations, highest first
  470. * is:
  471. * 100 Base-T Full Duplex
  472. * 100 Base-T Half Duplex
  473. * 10 Base-T Full Duplex
  474. * 10 Base-T Half Duplex
  475. *
  476. * We start a new timer now, after a successful auto negotiation status has
  477. * been detected. This timer just waits for the link-up bit to get set in
  478. * the BMCR of the DP83840. When this occurs we print a kernel log message
  479. * describing the link type in use and the fact that it is up.
  480. *
  481. * If a fatal error of some sort is signalled and detected in the interrupt
  482. * service routine, and the chip is reset, or the link is ifconfig'd down
  483. * and then back up, this entire process repeats itself all over again.
  484. */
  485. static int try_next_permutation(struct happy_meal *hp, void __iomem *tregs)
  486. {
  487. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  488. /* Downgrade from full to half duplex. Only possible
  489. * via ethtool.
  490. */
  491. if (hp->sw_bmcr & BMCR_FULLDPLX) {
  492. hp->sw_bmcr &= ~(BMCR_FULLDPLX);
  493. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  494. return 0;
  495. }
  496. /* Downgrade from 100 to 10. */
  497. if (hp->sw_bmcr & BMCR_SPEED100) {
  498. hp->sw_bmcr &= ~(BMCR_SPEED100);
  499. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  500. return 0;
  501. }
  502. /* We've tried everything. */
  503. return -1;
  504. }
  505. static void display_link_mode(struct happy_meal *hp, void __iomem *tregs)
  506. {
  507. printk(KERN_INFO "%s: Link is up using ", hp->dev->name);
  508. if (hp->tcvr_type == external)
  509. printk("external ");
  510. else
  511. printk("internal ");
  512. printk("transceiver at ");
  513. hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
  514. if (hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) {
  515. if (hp->sw_lpa & LPA_100FULL)
  516. printk("100Mb/s, Full Duplex.\n");
  517. else
  518. printk("100Mb/s, Half Duplex.\n");
  519. } else {
  520. if (hp->sw_lpa & LPA_10FULL)
  521. printk("10Mb/s, Full Duplex.\n");
  522. else
  523. printk("10Mb/s, Half Duplex.\n");
  524. }
  525. }
  526. static void display_forced_link_mode(struct happy_meal *hp, void __iomem *tregs)
  527. {
  528. printk(KERN_INFO "%s: Link has been forced up using ", hp->dev->name);
  529. if (hp->tcvr_type == external)
  530. printk("external ");
  531. else
  532. printk("internal ");
  533. printk("transceiver at ");
  534. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  535. if (hp->sw_bmcr & BMCR_SPEED100)
  536. printk("100Mb/s, ");
  537. else
  538. printk("10Mb/s, ");
  539. if (hp->sw_bmcr & BMCR_FULLDPLX)
  540. printk("Full Duplex.\n");
  541. else
  542. printk("Half Duplex.\n");
  543. }
  544. static int set_happy_link_modes(struct happy_meal *hp, void __iomem *tregs)
  545. {
  546. int full;
  547. /* All we care about is making sure the bigmac tx_cfg has a
  548. * proper duplex setting.
  549. */
  550. if (hp->timer_state == arbwait) {
  551. hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
  552. if (!(hp->sw_lpa & (LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL)))
  553. goto no_response;
  554. if (hp->sw_lpa & LPA_100FULL)
  555. full = 1;
  556. else if (hp->sw_lpa & LPA_100HALF)
  557. full = 0;
  558. else if (hp->sw_lpa & LPA_10FULL)
  559. full = 1;
  560. else
  561. full = 0;
  562. } else {
  563. /* Forcing a link mode. */
  564. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  565. if (hp->sw_bmcr & BMCR_FULLDPLX)
  566. full = 1;
  567. else
  568. full = 0;
  569. }
  570. /* Before changing other bits in the tx_cfg register, and in
  571. * general any of other the TX config registers too, you
  572. * must:
  573. * 1) Clear Enable
  574. * 2) Poll with reads until that bit reads back as zero
  575. * 3) Make TX configuration changes
  576. * 4) Set Enable once more
  577. */
  578. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  579. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
  580. ~(BIGMAC_TXCFG_ENABLE));
  581. while (hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) & BIGMAC_TXCFG_ENABLE)
  582. barrier();
  583. if (full) {
  584. hp->happy_flags |= HFLAG_FULL;
  585. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  586. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
  587. BIGMAC_TXCFG_FULLDPLX);
  588. } else {
  589. hp->happy_flags &= ~(HFLAG_FULL);
  590. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  591. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
  592. ~(BIGMAC_TXCFG_FULLDPLX));
  593. }
  594. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  595. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
  596. BIGMAC_TXCFG_ENABLE);
  597. return 0;
  598. no_response:
  599. return 1;
  600. }
  601. static int happy_meal_init(struct happy_meal *hp);
  602. static int is_lucent_phy(struct happy_meal *hp)
  603. {
  604. void __iomem *tregs = hp->tcvregs;
  605. unsigned short mr2, mr3;
  606. int ret = 0;
  607. mr2 = happy_meal_tcvr_read(hp, tregs, 2);
  608. mr3 = happy_meal_tcvr_read(hp, tregs, 3);
  609. if ((mr2 & 0xffff) == 0x0180 &&
  610. ((mr3 & 0xffff) >> 10) == 0x1d)
  611. ret = 1;
  612. return ret;
  613. }
  614. static void happy_meal_timer(unsigned long data)
  615. {
  616. struct happy_meal *hp = (struct happy_meal *) data;
  617. void __iomem *tregs = hp->tcvregs;
  618. int restart_timer = 0;
  619. spin_lock_irq(&hp->happy_lock);
  620. hp->timer_ticks++;
  621. switch(hp->timer_state) {
  622. case arbwait:
  623. /* Only allow for 5 ticks, thats 10 seconds and much too
  624. * long to wait for arbitration to complete.
  625. */
  626. if (hp->timer_ticks >= 10) {
  627. /* Enter force mode. */
  628. do_force_mode:
  629. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  630. printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful, trying force link mode\n",
  631. hp->dev->name);
  632. hp->sw_bmcr = BMCR_SPEED100;
  633. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  634. if (!is_lucent_phy(hp)) {
  635. /* OK, seems we need do disable the transceiver for the first
  636. * tick to make sure we get an accurate link state at the
  637. * second tick.
  638. */
  639. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
  640. hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
  641. happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG, hp->sw_csconfig);
  642. }
  643. hp->timer_state = ltrywait;
  644. hp->timer_ticks = 0;
  645. restart_timer = 1;
  646. } else {
  647. /* Anything interesting happen? */
  648. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  649. if (hp->sw_bmsr & BMSR_ANEGCOMPLETE) {
  650. int ret;
  651. /* Just what we've been waiting for... */
  652. ret = set_happy_link_modes(hp, tregs);
  653. if (ret) {
  654. /* Ooops, something bad happened, go to force
  655. * mode.
  656. *
  657. * XXX Broken hubs which don't support 802.3u
  658. * XXX auto-negotiation make this happen as well.
  659. */
  660. goto do_force_mode;
  661. }
  662. /* Success, at least so far, advance our state engine. */
  663. hp->timer_state = lupwait;
  664. restart_timer = 1;
  665. } else {
  666. restart_timer = 1;
  667. }
  668. }
  669. break;
  670. case lupwait:
  671. /* Auto negotiation was successful and we are awaiting a
  672. * link up status. I have decided to let this timer run
  673. * forever until some sort of error is signalled, reporting
  674. * a message to the user at 10 second intervals.
  675. */
  676. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  677. if (hp->sw_bmsr & BMSR_LSTATUS) {
  678. /* Wheee, it's up, display the link mode in use and put
  679. * the timer to sleep.
  680. */
  681. display_link_mode(hp, tregs);
  682. hp->timer_state = asleep;
  683. restart_timer = 0;
  684. } else {
  685. if (hp->timer_ticks >= 10) {
  686. printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
  687. "not completely up.\n", hp->dev->name);
  688. hp->timer_ticks = 0;
  689. restart_timer = 1;
  690. } else {
  691. restart_timer = 1;
  692. }
  693. }
  694. break;
  695. case ltrywait:
  696. /* Making the timeout here too long can make it take
  697. * annoyingly long to attempt all of the link mode
  698. * permutations, but then again this is essentially
  699. * error recovery code for the most part.
  700. */
  701. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  702. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
  703. if (hp->timer_ticks == 1) {
  704. if (!is_lucent_phy(hp)) {
  705. /* Re-enable transceiver, we'll re-enable the transceiver next
  706. * tick, then check link state on the following tick.
  707. */
  708. hp->sw_csconfig |= CSCONFIG_TCVDISAB;
  709. happy_meal_tcvr_write(hp, tregs,
  710. DP83840_CSCONFIG, hp->sw_csconfig);
  711. }
  712. restart_timer = 1;
  713. break;
  714. }
  715. if (hp->timer_ticks == 2) {
  716. if (!is_lucent_phy(hp)) {
  717. hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
  718. happy_meal_tcvr_write(hp, tregs,
  719. DP83840_CSCONFIG, hp->sw_csconfig);
  720. }
  721. restart_timer = 1;
  722. break;
  723. }
  724. if (hp->sw_bmsr & BMSR_LSTATUS) {
  725. /* Force mode selection success. */
  726. display_forced_link_mode(hp, tregs);
  727. set_happy_link_modes(hp, tregs); /* XXX error? then what? */
  728. hp->timer_state = asleep;
  729. restart_timer = 0;
  730. } else {
  731. if (hp->timer_ticks >= 4) { /* 6 seconds or so... */
  732. int ret;
  733. ret = try_next_permutation(hp, tregs);
  734. if (ret == -1) {
  735. /* Aieee, tried them all, reset the
  736. * chip and try all over again.
  737. */
  738. /* Let the user know... */
  739. printk(KERN_NOTICE "%s: Link down, cable problem?\n",
  740. hp->dev->name);
  741. ret = happy_meal_init(hp);
  742. if (ret) {
  743. /* ho hum... */
  744. printk(KERN_ERR "%s: Error, cannot re-init the "
  745. "Happy Meal.\n", hp->dev->name);
  746. }
  747. goto out;
  748. }
  749. if (!is_lucent_phy(hp)) {
  750. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
  751. DP83840_CSCONFIG);
  752. hp->sw_csconfig |= CSCONFIG_TCVDISAB;
  753. happy_meal_tcvr_write(hp, tregs,
  754. DP83840_CSCONFIG, hp->sw_csconfig);
  755. }
  756. hp->timer_ticks = 0;
  757. restart_timer = 1;
  758. } else {
  759. restart_timer = 1;
  760. }
  761. }
  762. break;
  763. case asleep:
  764. default:
  765. /* Can't happens.... */
  766. printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
  767. hp->dev->name);
  768. restart_timer = 0;
  769. hp->timer_ticks = 0;
  770. hp->timer_state = asleep; /* foo on you */
  771. break;
  772. };
  773. if (restart_timer) {
  774. hp->happy_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
  775. add_timer(&hp->happy_timer);
  776. }
  777. out:
  778. spin_unlock_irq(&hp->happy_lock);
  779. }
  780. #define TX_RESET_TRIES 32
  781. #define RX_RESET_TRIES 32
  782. /* hp->happy_lock must be held */
  783. static void happy_meal_tx_reset(struct happy_meal *hp, void __iomem *bregs)
  784. {
  785. int tries = TX_RESET_TRIES;
  786. HMD(("happy_meal_tx_reset: reset, "));
  787. /* Would you like to try our SMCC Delux? */
  788. hme_write32(hp, bregs + BMAC_TXSWRESET, 0);
  789. while ((hme_read32(hp, bregs + BMAC_TXSWRESET) & 1) && --tries)
  790. udelay(20);
  791. /* Lettuce, tomato, buggy hardware (no extra charge)? */
  792. if (!tries)
  793. printk(KERN_ERR "happy meal: Transceiver BigMac ATTACK!");
  794. /* Take care. */
  795. HMD(("done\n"));
  796. }
  797. /* hp->happy_lock must be held */
  798. static void happy_meal_rx_reset(struct happy_meal *hp, void __iomem *bregs)
  799. {
  800. int tries = RX_RESET_TRIES;
  801. HMD(("happy_meal_rx_reset: reset, "));
  802. /* We have a special on GNU/Viking hardware bugs today. */
  803. hme_write32(hp, bregs + BMAC_RXSWRESET, 0);
  804. while ((hme_read32(hp, bregs + BMAC_RXSWRESET) & 1) && --tries)
  805. udelay(20);
  806. /* Will that be all? */
  807. if (!tries)
  808. printk(KERN_ERR "happy meal: Receiver BigMac ATTACK!");
  809. /* Don't forget your vik_1137125_wa. Have a nice day. */
  810. HMD(("done\n"));
  811. }
  812. #define STOP_TRIES 16
  813. /* hp->happy_lock must be held */
  814. static void happy_meal_stop(struct happy_meal *hp, void __iomem *gregs)
  815. {
  816. int tries = STOP_TRIES;
  817. HMD(("happy_meal_stop: reset, "));
  818. /* We're consolidating our STB products, it's your lucky day. */
  819. hme_write32(hp, gregs + GREG_SWRESET, GREG_RESET_ALL);
  820. while (hme_read32(hp, gregs + GREG_SWRESET) && --tries)
  821. udelay(20);
  822. /* Come back next week when we are "Sun Microelectronics". */
  823. if (!tries)
  824. printk(KERN_ERR "happy meal: Fry guys.");
  825. /* Remember: "Different name, same old buggy as shit hardware." */
  826. HMD(("done\n"));
  827. }
  828. /* hp->happy_lock must be held */
  829. static void happy_meal_get_counters(struct happy_meal *hp, void __iomem *bregs)
  830. {
  831. struct net_device_stats *stats = &hp->net_stats;
  832. stats->rx_crc_errors += hme_read32(hp, bregs + BMAC_RCRCECTR);
  833. hme_write32(hp, bregs + BMAC_RCRCECTR, 0);
  834. stats->rx_frame_errors += hme_read32(hp, bregs + BMAC_UNALECTR);
  835. hme_write32(hp, bregs + BMAC_UNALECTR, 0);
  836. stats->rx_length_errors += hme_read32(hp, bregs + BMAC_GLECTR);
  837. hme_write32(hp, bregs + BMAC_GLECTR, 0);
  838. stats->tx_aborted_errors += hme_read32(hp, bregs + BMAC_EXCTR);
  839. stats->collisions +=
  840. (hme_read32(hp, bregs + BMAC_EXCTR) +
  841. hme_read32(hp, bregs + BMAC_LTCTR));
  842. hme_write32(hp, bregs + BMAC_EXCTR, 0);
  843. hme_write32(hp, bregs + BMAC_LTCTR, 0);
  844. }
  845. /* hp->happy_lock must be held */
  846. static void happy_meal_poll_stop(struct happy_meal *hp, void __iomem *tregs)
  847. {
  848. ASD(("happy_meal_poll_stop: "));
  849. /* If polling disabled or not polling already, nothing to do. */
  850. if ((hp->happy_flags & (HFLAG_POLLENABLE | HFLAG_POLL)) !=
  851. (HFLAG_POLLENABLE | HFLAG_POLL)) {
  852. HMD(("not polling, return\n"));
  853. return;
  854. }
  855. /* Shut up the MIF. */
  856. ASD(("were polling, mif ints off, "));
  857. hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
  858. /* Turn off polling. */
  859. ASD(("polling off, "));
  860. hme_write32(hp, tregs + TCVR_CFG,
  861. hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_PENABLE));
  862. /* We are no longer polling. */
  863. hp->happy_flags &= ~(HFLAG_POLL);
  864. /* Let the bits set. */
  865. udelay(200);
  866. ASD(("done\n"));
  867. }
  868. /* Only Sun can take such nice parts and fuck up the programming interface
  869. * like this. Good job guys...
  870. */
  871. #define TCVR_RESET_TRIES 16 /* It should reset quickly */
  872. #define TCVR_UNISOLATE_TRIES 32 /* Dis-isolation can take longer. */
  873. /* hp->happy_lock must be held */
  874. static int happy_meal_tcvr_reset(struct happy_meal *hp, void __iomem *tregs)
  875. {
  876. u32 tconfig;
  877. int result, tries = TCVR_RESET_TRIES;
  878. tconfig = hme_read32(hp, tregs + TCVR_CFG);
  879. ASD(("happy_meal_tcvr_reset: tcfg<%08lx> ", tconfig));
  880. if (hp->tcvr_type == external) {
  881. ASD(("external<"));
  882. hme_write32(hp, tregs + TCVR_CFG, tconfig & ~(TCV_CFG_PSELECT));
  883. hp->tcvr_type = internal;
  884. hp->paddr = TCV_PADDR_ITX;
  885. ASD(("ISOLATE,"));
  886. happy_meal_tcvr_write(hp, tregs, MII_BMCR,
  887. (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
  888. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  889. if (result == TCVR_FAILURE) {
  890. ASD(("phyread_fail>\n"));
  891. return -1;
  892. }
  893. ASD(("phyread_ok,PSELECT>"));
  894. hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
  895. hp->tcvr_type = external;
  896. hp->paddr = TCV_PADDR_ETX;
  897. } else {
  898. if (tconfig & TCV_CFG_MDIO1) {
  899. ASD(("internal<PSELECT,"));
  900. hme_write32(hp, tregs + TCVR_CFG, (tconfig | TCV_CFG_PSELECT));
  901. ASD(("ISOLATE,"));
  902. happy_meal_tcvr_write(hp, tregs, MII_BMCR,
  903. (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
  904. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  905. if (result == TCVR_FAILURE) {
  906. ASD(("phyread_fail>\n"));
  907. return -1;
  908. }
  909. ASD(("phyread_ok,~PSELECT>"));
  910. hme_write32(hp, tregs + TCVR_CFG, (tconfig & ~(TCV_CFG_PSELECT)));
  911. hp->tcvr_type = internal;
  912. hp->paddr = TCV_PADDR_ITX;
  913. }
  914. }
  915. ASD(("BMCR_RESET "));
  916. happy_meal_tcvr_write(hp, tregs, MII_BMCR, BMCR_RESET);
  917. while (--tries) {
  918. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  919. if (result == TCVR_FAILURE)
  920. return -1;
  921. hp->sw_bmcr = result;
  922. if (!(result & BMCR_RESET))
  923. break;
  924. udelay(20);
  925. }
  926. if (!tries) {
  927. ASD(("BMCR RESET FAILED!\n"));
  928. return -1;
  929. }
  930. ASD(("RESET_OK\n"));
  931. /* Get fresh copies of the PHY registers. */
  932. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  933. hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
  934. hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
  935. hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
  936. ASD(("UNISOLATE"));
  937. hp->sw_bmcr &= ~(BMCR_ISOLATE);
  938. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  939. tries = TCVR_UNISOLATE_TRIES;
  940. while (--tries) {
  941. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  942. if (result == TCVR_FAILURE)
  943. return -1;
  944. if (!(result & BMCR_ISOLATE))
  945. break;
  946. udelay(20);
  947. }
  948. if (!tries) {
  949. ASD((" FAILED!\n"));
  950. return -1;
  951. }
  952. ASD((" SUCCESS and CSCONFIG_DFBYPASS\n"));
  953. if (!is_lucent_phy(hp)) {
  954. result = happy_meal_tcvr_read(hp, tregs,
  955. DP83840_CSCONFIG);
  956. happy_meal_tcvr_write(hp, tregs,
  957. DP83840_CSCONFIG, (result | CSCONFIG_DFBYPASS));
  958. }
  959. return 0;
  960. }
  961. /* Figure out whether we have an internal or external transceiver.
  962. *
  963. * hp->happy_lock must be held
  964. */
  965. static void happy_meal_transceiver_check(struct happy_meal *hp, void __iomem *tregs)
  966. {
  967. unsigned long tconfig = hme_read32(hp, tregs + TCVR_CFG);
  968. ASD(("happy_meal_transceiver_check: tcfg=%08lx ", tconfig));
  969. if (hp->happy_flags & HFLAG_POLL) {
  970. /* If we are polling, we must stop to get the transceiver type. */
  971. ASD(("<polling> "));
  972. if (hp->tcvr_type == internal) {
  973. if (tconfig & TCV_CFG_MDIO1) {
  974. ASD(("<internal> <poll stop> "));
  975. happy_meal_poll_stop(hp, tregs);
  976. hp->paddr = TCV_PADDR_ETX;
  977. hp->tcvr_type = external;
  978. ASD(("<external>\n"));
  979. tconfig &= ~(TCV_CFG_PENABLE);
  980. tconfig |= TCV_CFG_PSELECT;
  981. hme_write32(hp, tregs + TCVR_CFG, tconfig);
  982. }
  983. } else {
  984. if (hp->tcvr_type == external) {
  985. ASD(("<external> "));
  986. if (!(hme_read32(hp, tregs + TCVR_STATUS) >> 16)) {
  987. ASD(("<poll stop> "));
  988. happy_meal_poll_stop(hp, tregs);
  989. hp->paddr = TCV_PADDR_ITX;
  990. hp->tcvr_type = internal;
  991. ASD(("<internal>\n"));
  992. hme_write32(hp, tregs + TCVR_CFG,
  993. hme_read32(hp, tregs + TCVR_CFG) &
  994. ~(TCV_CFG_PSELECT));
  995. }
  996. ASD(("\n"));
  997. } else {
  998. ASD(("<none>\n"));
  999. }
  1000. }
  1001. } else {
  1002. u32 reread = hme_read32(hp, tregs + TCVR_CFG);
  1003. /* Else we can just work off of the MDIO bits. */
  1004. ASD(("<not polling> "));
  1005. if (reread & TCV_CFG_MDIO1) {
  1006. hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
  1007. hp->paddr = TCV_PADDR_ETX;
  1008. hp->tcvr_type = external;
  1009. ASD(("<external>\n"));
  1010. } else {
  1011. if (reread & TCV_CFG_MDIO0) {
  1012. hme_write32(hp, tregs + TCVR_CFG,
  1013. tconfig & ~(TCV_CFG_PSELECT));
  1014. hp->paddr = TCV_PADDR_ITX;
  1015. hp->tcvr_type = internal;
  1016. ASD(("<internal>\n"));
  1017. } else {
  1018. printk(KERN_ERR "happy meal: Transceiver and a coke please.");
  1019. hp->tcvr_type = none; /* Grrr... */
  1020. ASD(("<none>\n"));
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /* The receive ring buffers are a bit tricky to get right. Here goes...
  1026. *
  1027. * The buffers we dma into must be 64 byte aligned. So we use a special
  1028. * alloc_skb() routine for the happy meal to allocate 64 bytes more than
  1029. * we really need.
  1030. *
  1031. * We use skb_reserve() to align the data block we get in the skb. We
  1032. * also program the etxregs->cfg register to use an offset of 2. This
  1033. * imperical constant plus the ethernet header size will always leave
  1034. * us with a nicely aligned ip header once we pass things up to the
  1035. * protocol layers.
  1036. *
  1037. * The numbers work out to:
  1038. *
  1039. * Max ethernet frame size 1518
  1040. * Ethernet header size 14
  1041. * Happy Meal base offset 2
  1042. *
  1043. * Say a skb data area is at 0xf001b010, and its size alloced is
  1044. * (ETH_FRAME_LEN + 64 + 2) = (1514 + 64 + 2) = 1580 bytes.
  1045. *
  1046. * First our alloc_skb() routine aligns the data base to a 64 byte
  1047. * boundary. We now have 0xf001b040 as our skb data address. We
  1048. * plug this into the receive descriptor address.
  1049. *
  1050. * Next, we skb_reserve() 2 bytes to account for the Happy Meal offset.
  1051. * So now the data we will end up looking at starts at 0xf001b042. When
  1052. * the packet arrives, we will check out the size received and subtract
  1053. * this from the skb->length. Then we just pass the packet up to the
  1054. * protocols as is, and allocate a new skb to replace this slot we have
  1055. * just received from.
  1056. *
  1057. * The ethernet layer will strip the ether header from the front of the
  1058. * skb we just sent to it, this leaves us with the ip header sitting
  1059. * nicely aligned at 0xf001b050. Also, for tcp and udp packets the
  1060. * Happy Meal has even checksummed the tcp/udp data for us. The 16
  1061. * bit checksum is obtained from the low bits of the receive descriptor
  1062. * flags, thus:
  1063. *
  1064. * skb->csum = rxd->rx_flags & 0xffff;
  1065. * skb->ip_summed = CHECKSUM_COMPLETE;
  1066. *
  1067. * before sending off the skb to the protocols, and we are good as gold.
  1068. */
  1069. static void happy_meal_clean_rings(struct happy_meal *hp)
  1070. {
  1071. int i;
  1072. for (i = 0; i < RX_RING_SIZE; i++) {
  1073. if (hp->rx_skbs[i] != NULL) {
  1074. struct sk_buff *skb = hp->rx_skbs[i];
  1075. struct happy_meal_rxd *rxd;
  1076. u32 dma_addr;
  1077. rxd = &hp->happy_block->happy_meal_rxd[i];
  1078. dma_addr = hme_read_desc32(hp, &rxd->rx_addr);
  1079. hme_dma_unmap(hp, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE);
  1080. dev_kfree_skb_any(skb);
  1081. hp->rx_skbs[i] = NULL;
  1082. }
  1083. }
  1084. for (i = 0; i < TX_RING_SIZE; i++) {
  1085. if (hp->tx_skbs[i] != NULL) {
  1086. struct sk_buff *skb = hp->tx_skbs[i];
  1087. struct happy_meal_txd *txd;
  1088. u32 dma_addr;
  1089. int frag;
  1090. hp->tx_skbs[i] = NULL;
  1091. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  1092. txd = &hp->happy_block->happy_meal_txd[i];
  1093. dma_addr = hme_read_desc32(hp, &txd->tx_addr);
  1094. hme_dma_unmap(hp, dma_addr,
  1095. (hme_read_desc32(hp, &txd->tx_flags)
  1096. & TXFLAG_SIZE),
  1097. DMA_TODEVICE);
  1098. if (frag != skb_shinfo(skb)->nr_frags)
  1099. i++;
  1100. }
  1101. dev_kfree_skb_any(skb);
  1102. }
  1103. }
  1104. }
  1105. /* hp->happy_lock must be held */
  1106. static void happy_meal_init_rings(struct happy_meal *hp)
  1107. {
  1108. struct hmeal_init_block *hb = hp->happy_block;
  1109. struct net_device *dev = hp->dev;
  1110. int i;
  1111. HMD(("happy_meal_init_rings: counters to zero, "));
  1112. hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;
  1113. /* Free any skippy bufs left around in the rings. */
  1114. HMD(("clean, "));
  1115. happy_meal_clean_rings(hp);
  1116. /* Now get new skippy bufs for the receive ring. */
  1117. HMD(("init rxring, "));
  1118. for (i = 0; i < RX_RING_SIZE; i++) {
  1119. struct sk_buff *skb;
  1120. skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  1121. if (!skb) {
  1122. hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
  1123. continue;
  1124. }
  1125. hp->rx_skbs[i] = skb;
  1126. skb->dev = dev;
  1127. /* Because we reserve afterwards. */
  1128. skb_put(skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
  1129. hme_write_rxd(hp, &hb->happy_meal_rxd[i],
  1130. (RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16)),
  1131. hme_dma_map(hp, skb->data, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE));
  1132. skb_reserve(skb, RX_OFFSET);
  1133. }
  1134. HMD(("init txring, "));
  1135. for (i = 0; i < TX_RING_SIZE; i++)
  1136. hme_write_txd(hp, &hb->happy_meal_txd[i], 0, 0);
  1137. HMD(("done\n"));
  1138. }
  1139. /* hp->happy_lock must be held */
  1140. static void happy_meal_begin_auto_negotiation(struct happy_meal *hp,
  1141. void __iomem *tregs,
  1142. struct ethtool_cmd *ep)
  1143. {
  1144. int timeout;
  1145. /* Read all of the registers we are interested in now. */
  1146. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  1147. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  1148. hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
  1149. hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
  1150. /* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */
  1151. hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
  1152. if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
  1153. /* Advertise everything we can support. */
  1154. if (hp->sw_bmsr & BMSR_10HALF)
  1155. hp->sw_advertise |= (ADVERTISE_10HALF);
  1156. else
  1157. hp->sw_advertise &= ~(ADVERTISE_10HALF);
  1158. if (hp->sw_bmsr & BMSR_10FULL)
  1159. hp->sw_advertise |= (ADVERTISE_10FULL);
  1160. else
  1161. hp->sw_advertise &= ~(ADVERTISE_10FULL);
  1162. if (hp->sw_bmsr & BMSR_100HALF)
  1163. hp->sw_advertise |= (ADVERTISE_100HALF);
  1164. else
  1165. hp->sw_advertise &= ~(ADVERTISE_100HALF);
  1166. if (hp->sw_bmsr & BMSR_100FULL)
  1167. hp->sw_advertise |= (ADVERTISE_100FULL);
  1168. else
  1169. hp->sw_advertise &= ~(ADVERTISE_100FULL);
  1170. happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
  1171. /* XXX Currently no Happy Meal cards I know off support 100BaseT4,
  1172. * XXX and this is because the DP83840 does not support it, changes
  1173. * XXX would need to be made to the tx/rx logic in the driver as well
  1174. * XXX so I completely skip checking for it in the BMSR for now.
  1175. */
  1176. #ifdef AUTO_SWITCH_DEBUG
  1177. ASD(("%s: Advertising [ ", hp->dev->name));
  1178. if (hp->sw_advertise & ADVERTISE_10HALF)
  1179. ASD(("10H "));
  1180. if (hp->sw_advertise & ADVERTISE_10FULL)
  1181. ASD(("10F "));
  1182. if (hp->sw_advertise & ADVERTISE_100HALF)
  1183. ASD(("100H "));
  1184. if (hp->sw_advertise & ADVERTISE_100FULL)
  1185. ASD(("100F "));
  1186. #endif
  1187. /* Enable Auto-Negotiation, this is usually on already... */
  1188. hp->sw_bmcr |= BMCR_ANENABLE;
  1189. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1190. /* Restart it to make sure it is going. */
  1191. hp->sw_bmcr |= BMCR_ANRESTART;
  1192. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1193. /* BMCR_ANRESTART self clears when the process has begun. */
  1194. timeout = 64; /* More than enough. */
  1195. while (--timeout) {
  1196. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  1197. if (!(hp->sw_bmcr & BMCR_ANRESTART))
  1198. break; /* got it. */
  1199. udelay(10);
  1200. }
  1201. if (!timeout) {
  1202. printk(KERN_ERR "%s: Happy Meal would not start auto negotiation "
  1203. "BMCR=0x%04x\n", hp->dev->name, hp->sw_bmcr);
  1204. printk(KERN_NOTICE "%s: Performing force link detection.\n",
  1205. hp->dev->name);
  1206. goto force_link;
  1207. } else {
  1208. hp->timer_state = arbwait;
  1209. }
  1210. } else {
  1211. force_link:
  1212. /* Force the link up, trying first a particular mode.
  1213. * Either we are here at the request of ethtool or
  1214. * because the Happy Meal would not start to autoneg.
  1215. */
  1216. /* Disable auto-negotiation in BMCR, enable the duplex and
  1217. * speed setting, init the timer state machine, and fire it off.
  1218. */
  1219. if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
  1220. hp->sw_bmcr = BMCR_SPEED100;
  1221. } else {
  1222. if (ep->speed == SPEED_100)
  1223. hp->sw_bmcr = BMCR_SPEED100;
  1224. else
  1225. hp->sw_bmcr = 0;
  1226. if (ep->duplex == DUPLEX_FULL)
  1227. hp->sw_bmcr |= BMCR_FULLDPLX;
  1228. }
  1229. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1230. if (!is_lucent_phy(hp)) {
  1231. /* OK, seems we need do disable the transceiver for the first
  1232. * tick to make sure we get an accurate link state at the
  1233. * second tick.
  1234. */
  1235. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
  1236. DP83840_CSCONFIG);
  1237. hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
  1238. happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
  1239. hp->sw_csconfig);
  1240. }
  1241. hp->timer_state = ltrywait;
  1242. }
  1243. hp->timer_ticks = 0;
  1244. hp->happy_timer.expires = jiffies + (12 * HZ)/10; /* 1.2 sec. */
  1245. hp->happy_timer.data = (unsigned long) hp;
  1246. hp->happy_timer.function = &happy_meal_timer;
  1247. add_timer(&hp->happy_timer);
  1248. }
  1249. /* hp->happy_lock must be held */
  1250. static int happy_meal_init(struct happy_meal *hp)
  1251. {
  1252. void __iomem *gregs = hp->gregs;
  1253. void __iomem *etxregs = hp->etxregs;
  1254. void __iomem *erxregs = hp->erxregs;
  1255. void __iomem *bregs = hp->bigmacregs;
  1256. void __iomem *tregs = hp->tcvregs;
  1257. u32 regtmp, rxcfg;
  1258. unsigned char *e = &hp->dev->dev_addr[0];
  1259. /* If auto-negotiation timer is running, kill it. */
  1260. del_timer(&hp->happy_timer);
  1261. HMD(("happy_meal_init: happy_flags[%08x] ",
  1262. hp->happy_flags));
  1263. if (!(hp->happy_flags & HFLAG_INIT)) {
  1264. HMD(("set HFLAG_INIT, "));
  1265. hp->happy_flags |= HFLAG_INIT;
  1266. happy_meal_get_counters(hp, bregs);
  1267. }
  1268. /* Stop polling. */
  1269. HMD(("to happy_meal_poll_stop\n"));
  1270. happy_meal_poll_stop(hp, tregs);
  1271. /* Stop transmitter and receiver. */
  1272. HMD(("happy_meal_init: to happy_meal_stop\n"));
  1273. happy_meal_stop(hp, gregs);
  1274. /* Alloc and reset the tx/rx descriptor chains. */
  1275. HMD(("happy_meal_init: to happy_meal_init_rings\n"));
  1276. happy_meal_init_rings(hp);
  1277. /* Shut up the MIF. */
  1278. HMD(("happy_meal_init: Disable all MIF irqs (old[%08x]), ",
  1279. hme_read32(hp, tregs + TCVR_IMASK)));
  1280. hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
  1281. /* See if we can enable the MIF frame on this card to speak to the DP83840. */
  1282. if (hp->happy_flags & HFLAG_FENABLE) {
  1283. HMD(("use frame old[%08x], ",
  1284. hme_read32(hp, tregs + TCVR_CFG)));
  1285. hme_write32(hp, tregs + TCVR_CFG,
  1286. hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
  1287. } else {
  1288. HMD(("use bitbang old[%08x], ",
  1289. hme_read32(hp, tregs + TCVR_CFG)));
  1290. hme_write32(hp, tregs + TCVR_CFG,
  1291. hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
  1292. }
  1293. /* Check the state of the transceiver. */
  1294. HMD(("to happy_meal_transceiver_check\n"));
  1295. happy_meal_transceiver_check(hp, tregs);
  1296. /* Put the Big Mac into a sane state. */
  1297. HMD(("happy_meal_init: "));
  1298. switch(hp->tcvr_type) {
  1299. case none:
  1300. /* Cannot operate if we don't know the transceiver type! */
  1301. HMD(("AAIEEE no transceiver type, EAGAIN"));
  1302. return -EAGAIN;
  1303. case internal:
  1304. /* Using the MII buffers. */
  1305. HMD(("internal, using MII, "));
  1306. hme_write32(hp, bregs + BMAC_XIFCFG, 0);
  1307. break;
  1308. case external:
  1309. /* Not using the MII, disable it. */
  1310. HMD(("external, disable MII, "));
  1311. hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
  1312. break;
  1313. };
  1314. if (happy_meal_tcvr_reset(hp, tregs))
  1315. return -EAGAIN;
  1316. /* Reset the Happy Meal Big Mac transceiver and the receiver. */
  1317. HMD(("tx/rx reset, "));
  1318. happy_meal_tx_reset(hp, bregs);
  1319. happy_meal_rx_reset(hp, bregs);
  1320. /* Set jam size and inter-packet gaps to reasonable defaults. */
  1321. HMD(("jsize/ipg1/ipg2, "));
  1322. hme_write32(hp, bregs + BMAC_JSIZE, DEFAULT_JAMSIZE);
  1323. hme_write32(hp, bregs + BMAC_IGAP1, DEFAULT_IPG1);
  1324. hme_write32(hp, bregs + BMAC_IGAP2, DEFAULT_IPG2);
  1325. /* Load up the MAC address and random seed. */
  1326. HMD(("rseed/macaddr, "));
  1327. /* The docs recommend to use the 10LSB of our MAC here. */
  1328. hme_write32(hp, bregs + BMAC_RSEED, ((e[5] | e[4]<<8)&0x3ff));
  1329. hme_write32(hp, bregs + BMAC_MACADDR2, ((e[4] << 8) | e[5]));
  1330. hme_write32(hp, bregs + BMAC_MACADDR1, ((e[2] << 8) | e[3]));
  1331. hme_write32(hp, bregs + BMAC_MACADDR0, ((e[0] << 8) | e[1]));
  1332. HMD(("htable, "));
  1333. if ((hp->dev->flags & IFF_ALLMULTI) ||
  1334. (hp->dev->mc_count > 64)) {
  1335. hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
  1336. hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
  1337. hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
  1338. hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
  1339. } else if ((hp->dev->flags & IFF_PROMISC) == 0) {
  1340. u16 hash_table[4];
  1341. struct dev_mc_list *dmi = hp->dev->mc_list;
  1342. char *addrs;
  1343. int i;
  1344. u32 crc;
  1345. for (i = 0; i < 4; i++)
  1346. hash_table[i] = 0;
  1347. for (i = 0; i < hp->dev->mc_count; i++) {
  1348. addrs = dmi->dmi_addr;
  1349. dmi = dmi->next;
  1350. if (!(*addrs & 1))
  1351. continue;
  1352. crc = ether_crc_le(6, addrs);
  1353. crc >>= 26;
  1354. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  1355. }
  1356. hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
  1357. hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
  1358. hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
  1359. hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
  1360. } else {
  1361. hme_write32(hp, bregs + BMAC_HTABLE3, 0);
  1362. hme_write32(hp, bregs + BMAC_HTABLE2, 0);
  1363. hme_write32(hp, bregs + BMAC_HTABLE1, 0);
  1364. hme_write32(hp, bregs + BMAC_HTABLE0, 0);
  1365. }
  1366. /* Set the RX and TX ring ptrs. */
  1367. HMD(("ring ptrs rxr[%08x] txr[%08x]\n",
  1368. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)),
  1369. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0))));
  1370. hme_write32(hp, erxregs + ERX_RING,
  1371. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)));
  1372. hme_write32(hp, etxregs + ETX_RING,
  1373. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));
  1374. /* Parity issues in the ERX unit of some HME revisions can cause some
  1375. * registers to not be written unless their parity is even. Detect such
  1376. * lost writes and simply rewrite with a low bit set (which will be ignored
  1377. * since the rxring needs to be 2K aligned).
  1378. */
  1379. if (hme_read32(hp, erxregs + ERX_RING) !=
  1380. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)))
  1381. hme_write32(hp, erxregs + ERX_RING,
  1382. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0))
  1383. | 0x4);
  1384. /* Set the supported burst sizes. */
  1385. HMD(("happy_meal_init: old[%08x] bursts<",
  1386. hme_read32(hp, gregs + GREG_CFG)));
  1387. #ifndef CONFIG_SPARC
  1388. /* It is always PCI and can handle 64byte bursts. */
  1389. hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST64);
  1390. #else
  1391. if ((hp->happy_bursts & DMA_BURST64) &&
  1392. ((hp->happy_flags & HFLAG_PCI) != 0
  1393. #ifdef CONFIG_SBUS
  1394. || sbus_can_burst64(hp->happy_dev)
  1395. #endif
  1396. || 0)) {
  1397. u32 gcfg = GREG_CFG_BURST64;
  1398. /* I have no idea if I should set the extended
  1399. * transfer mode bit for Cheerio, so for now I
  1400. * do not. -DaveM
  1401. */
  1402. #ifdef CONFIG_SBUS
  1403. if ((hp->happy_flags & HFLAG_PCI) == 0 &&
  1404. sbus_can_dma_64bit(hp->happy_dev)) {
  1405. sbus_set_sbus64(hp->happy_dev,
  1406. hp->happy_bursts);
  1407. gcfg |= GREG_CFG_64BIT;
  1408. }
  1409. #endif
  1410. HMD(("64>"));
  1411. hme_write32(hp, gregs + GREG_CFG, gcfg);
  1412. } else if (hp->happy_bursts & DMA_BURST32) {
  1413. HMD(("32>"));
  1414. hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST32);
  1415. } else if (hp->happy_bursts & DMA_BURST16) {
  1416. HMD(("16>"));
  1417. hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST16);
  1418. } else {
  1419. HMD(("XXX>"));
  1420. hme_write32(hp, gregs + GREG_CFG, 0);
  1421. }
  1422. #endif /* CONFIG_SPARC */
  1423. /* Turn off interrupts we do not want to hear. */
  1424. HMD((", enable global interrupts, "));
  1425. hme_write32(hp, gregs + GREG_IMASK,
  1426. (GREG_IMASK_GOTFRAME | GREG_IMASK_RCNTEXP |
  1427. GREG_IMASK_SENTFRAME | GREG_IMASK_TXPERR));
  1428. /* Set the transmit ring buffer size. */
  1429. HMD(("tx rsize=%d oreg[%08x], ", (int)TX_RING_SIZE,
  1430. hme_read32(hp, etxregs + ETX_RSIZE)));
  1431. hme_write32(hp, etxregs + ETX_RSIZE, (TX_RING_SIZE >> ETX_RSIZE_SHIFT) - 1);
  1432. /* Enable transmitter DVMA. */
  1433. HMD(("tx dma enable old[%08x], ",
  1434. hme_read32(hp, etxregs + ETX_CFG)));
  1435. hme_write32(hp, etxregs + ETX_CFG,
  1436. hme_read32(hp, etxregs + ETX_CFG) | ETX_CFG_DMAENABLE);
  1437. /* This chip really rots, for the receiver sometimes when you
  1438. * write to its control registers not all the bits get there
  1439. * properly. I cannot think of a sane way to provide complete
  1440. * coverage for this hardware bug yet.
  1441. */
  1442. HMD(("erx regs bug old[%08x]\n",
  1443. hme_read32(hp, erxregs + ERX_CFG)));
  1444. hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
  1445. regtmp = hme_read32(hp, erxregs + ERX_CFG);
  1446. hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
  1447. if (hme_read32(hp, erxregs + ERX_CFG) != ERX_CFG_DEFAULT(RX_OFFSET)) {
  1448. printk(KERN_ERR "happy meal: Eieee, rx config register gets greasy fries.\n");
  1449. printk(KERN_ERR "happy meal: Trying to set %08x, reread gives %08x\n",
  1450. ERX_CFG_DEFAULT(RX_OFFSET), regtmp);
  1451. /* XXX Should return failure here... */
  1452. }
  1453. /* Enable Big Mac hash table filter. */
  1454. HMD(("happy_meal_init: enable hash rx_cfg_old[%08x], ",
  1455. hme_read32(hp, bregs + BMAC_RXCFG)));
  1456. rxcfg = BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_REJME;
  1457. if (hp->dev->flags & IFF_PROMISC)
  1458. rxcfg |= BIGMAC_RXCFG_PMISC;
  1459. hme_write32(hp, bregs + BMAC_RXCFG, rxcfg);
  1460. /* Let the bits settle in the chip. */
  1461. udelay(10);
  1462. /* Ok, configure the Big Mac transmitter. */
  1463. HMD(("BIGMAC init, "));
  1464. regtmp = 0;
  1465. if (hp->happy_flags & HFLAG_FULL)
  1466. regtmp |= BIGMAC_TXCFG_FULLDPLX;
  1467. /* Don't turn on the "don't give up" bit for now. It could cause hme
  1468. * to deadlock with the PHY if a Jabber occurs.
  1469. */
  1470. hme_write32(hp, bregs + BMAC_TXCFG, regtmp /*| BIGMAC_TXCFG_DGIVEUP*/);
  1471. /* Give up after 16 TX attempts. */
  1472. hme_write32(hp, bregs + BMAC_ALIMIT, 16);
  1473. /* Enable the output drivers no matter what. */
  1474. regtmp = BIGMAC_XCFG_ODENABLE;
  1475. /* If card can do lance mode, enable it. */
  1476. if (hp->happy_flags & HFLAG_LANCE)
  1477. regtmp |= (DEFAULT_IPG0 << 5) | BIGMAC_XCFG_LANCE;
  1478. /* Disable the MII buffers if using external transceiver. */
  1479. if (hp->tcvr_type == external)
  1480. regtmp |= BIGMAC_XCFG_MIIDISAB;
  1481. HMD(("XIF config old[%08x], ",
  1482. hme_read32(hp, bregs + BMAC_XIFCFG)));
  1483. hme_write32(hp, bregs + BMAC_XIFCFG, regtmp);
  1484. /* Start things up. */
  1485. HMD(("tx old[%08x] and rx [%08x] ON!\n",
  1486. hme_read32(hp, bregs + BMAC_TXCFG),
  1487. hme_read32(hp, bregs + BMAC_RXCFG)));
  1488. /* Set larger TX/RX size to allow for 802.1q */
  1489. hme_write32(hp, bregs + BMAC_TXMAX, ETH_FRAME_LEN + 8);
  1490. hme_write32(hp, bregs + BMAC_RXMAX, ETH_FRAME_LEN + 8);
  1491. hme_write32(hp, bregs + BMAC_TXCFG,
  1492. hme_read32(hp, bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE);
  1493. hme_write32(hp, bregs + BMAC_RXCFG,
  1494. hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE);
  1495. /* Get the autonegotiation started, and the watch timer ticking. */
  1496. happy_meal_begin_auto_negotiation(hp, tregs, NULL);
  1497. /* Success. */
  1498. return 0;
  1499. }
  1500. /* hp->happy_lock must be held */
  1501. static void happy_meal_set_initial_advertisement(struct happy_meal *hp)
  1502. {
  1503. void __iomem *tregs = hp->tcvregs;
  1504. void __iomem *bregs = hp->bigmacregs;
  1505. void __iomem *gregs = hp->gregs;
  1506. happy_meal_stop(hp, gregs);
  1507. hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
  1508. if (hp->happy_flags & HFLAG_FENABLE)
  1509. hme_write32(hp, tregs + TCVR_CFG,
  1510. hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
  1511. else
  1512. hme_write32(hp, tregs + TCVR_CFG,
  1513. hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
  1514. happy_meal_transceiver_check(hp, tregs);
  1515. switch(hp->tcvr_type) {
  1516. case none:
  1517. return;
  1518. case internal:
  1519. hme_write32(hp, bregs + BMAC_XIFCFG, 0);
  1520. break;
  1521. case external:
  1522. hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
  1523. break;
  1524. };
  1525. if (happy_meal_tcvr_reset(hp, tregs))
  1526. return;
  1527. /* Latch PHY registers as of now. */
  1528. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  1529. hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
  1530. /* Advertise everything we can support. */
  1531. if (hp->sw_bmsr & BMSR_10HALF)
  1532. hp->sw_advertise |= (ADVERTISE_10HALF);
  1533. else
  1534. hp->sw_advertise &= ~(ADVERTISE_10HALF);
  1535. if (hp->sw_bmsr & BMSR_10FULL)
  1536. hp->sw_advertise |= (ADVERTISE_10FULL);
  1537. else
  1538. hp->sw_advertise &= ~(ADVERTISE_10FULL);
  1539. if (hp->sw_bmsr & BMSR_100HALF)
  1540. hp->sw_advertise |= (ADVERTISE_100HALF);
  1541. else
  1542. hp->sw_advertise &= ~(ADVERTISE_100HALF);
  1543. if (hp->sw_bmsr & BMSR_100FULL)
  1544. hp->sw_advertise |= (ADVERTISE_100FULL);
  1545. else
  1546. hp->sw_advertise &= ~(ADVERTISE_100FULL);
  1547. /* Update the PHY advertisement register. */
  1548. happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
  1549. }
  1550. /* Once status is latched (by happy_meal_interrupt) it is cleared by
  1551. * the hardware, so we cannot re-read it and get a correct value.
  1552. *
  1553. * hp->happy_lock must be held
  1554. */
  1555. static int happy_meal_is_not_so_happy(struct happy_meal *hp, u32 status)
  1556. {
  1557. int reset = 0;
  1558. /* Only print messages for non-counter related interrupts. */
  1559. if (status & (GREG_STAT_STSTERR | GREG_STAT_TFIFO_UND |
  1560. GREG_STAT_MAXPKTERR | GREG_STAT_RXERR |
  1561. GREG_STAT_RXPERR | GREG_STAT_RXTERR | GREG_STAT_EOPERR |
  1562. GREG_STAT_MIFIRQ | GREG_STAT_TXEACK | GREG_STAT_TXLERR |
  1563. GREG_STAT_TXPERR | GREG_STAT_TXTERR | GREG_STAT_SLVERR |
  1564. GREG_STAT_SLVPERR))
  1565. printk(KERN_ERR "%s: Error interrupt for happy meal, status = %08x\n",
  1566. hp->dev->name, status);
  1567. if (status & GREG_STAT_RFIFOVF) {
  1568. /* Receive FIFO overflow is harmless and the hardware will take
  1569. care of it, just some packets are lost. Who cares. */
  1570. printk(KERN_DEBUG "%s: Happy Meal receive FIFO overflow.\n", hp->dev->name);
  1571. }
  1572. if (status & GREG_STAT_STSTERR) {
  1573. /* BigMAC SQE link test failed. */
  1574. printk(KERN_ERR "%s: Happy Meal BigMAC SQE test failed.\n", hp->dev->name);
  1575. reset = 1;
  1576. }
  1577. if (status & GREG_STAT_TFIFO_UND) {
  1578. /* Transmit FIFO underrun, again DMA error likely. */
  1579. printk(KERN_ERR "%s: Happy Meal transmitter FIFO underrun, DMA error.\n",
  1580. hp->dev->name);
  1581. reset = 1;
  1582. }
  1583. if (status & GREG_STAT_MAXPKTERR) {
  1584. /* Driver error, tried to transmit something larger
  1585. * than ethernet max mtu.
  1586. */
  1587. printk(KERN_ERR "%s: Happy Meal MAX Packet size error.\n", hp->dev->name);
  1588. reset = 1;
  1589. }
  1590. if (status & GREG_STAT_NORXD) {
  1591. /* This is harmless, it just means the system is
  1592. * quite loaded and the incoming packet rate was
  1593. * faster than the interrupt handler could keep up
  1594. * with.
  1595. */
  1596. printk(KERN_INFO "%s: Happy Meal out of receive "
  1597. "descriptors, packet dropped.\n",
  1598. hp->dev->name);
  1599. }
  1600. if (status & (GREG_STAT_RXERR|GREG_STAT_RXPERR|GREG_STAT_RXTERR)) {
  1601. /* All sorts of DMA receive errors. */
  1602. printk(KERN_ERR "%s: Happy Meal rx DMA errors [ ", hp->dev->name);
  1603. if (status & GREG_STAT_RXERR)
  1604. printk("GenericError ");
  1605. if (status & GREG_STAT_RXPERR)
  1606. printk("ParityError ");
  1607. if (status & GREG_STAT_RXTERR)
  1608. printk("RxTagBotch ");
  1609. printk("]\n");
  1610. reset = 1;
  1611. }
  1612. if (status & GREG_STAT_EOPERR) {
  1613. /* Driver bug, didn't set EOP bit in tx descriptor given
  1614. * to the happy meal.
  1615. */
  1616. printk(KERN_ERR "%s: EOP not set in happy meal transmit descriptor!\n",
  1617. hp->dev->name);
  1618. reset = 1;
  1619. }
  1620. if (status & GREG_STAT_MIFIRQ) {
  1621. /* MIF signalled an interrupt, were we polling it? */
  1622. printk(KERN_ERR "%s: Happy Meal MIF interrupt.\n", hp->dev->name);
  1623. }
  1624. if (status &
  1625. (GREG_STAT_TXEACK|GREG_STAT_TXLERR|GREG_STAT_TXPERR|GREG_STAT_TXTERR)) {
  1626. /* All sorts of transmit DMA errors. */
  1627. printk(KERN_ERR "%s: Happy Meal tx DMA errors [ ", hp->dev->name);
  1628. if (status & GREG_STAT_TXEACK)
  1629. printk("GenericError ");
  1630. if (status & GREG_STAT_TXLERR)
  1631. printk("LateError ");
  1632. if (status & GREG_STAT_TXPERR)
  1633. printk("ParityErro ");
  1634. if (status & GREG_STAT_TXTERR)
  1635. printk("TagBotch ");
  1636. printk("]\n");
  1637. reset = 1;
  1638. }
  1639. if (status & (GREG_STAT_SLVERR|GREG_STAT_SLVPERR)) {
  1640. /* Bus or parity error when cpu accessed happy meal registers
  1641. * or it's internal FIFO's. Should never see this.
  1642. */
  1643. printk(KERN_ERR "%s: Happy Meal register access SBUS slave (%s) error.\n",
  1644. hp->dev->name,
  1645. (status & GREG_STAT_SLVPERR) ? "parity" : "generic");
  1646. reset = 1;
  1647. }
  1648. if (reset) {
  1649. printk(KERN_NOTICE "%s: Resetting...\n", hp->dev->name);
  1650. happy_meal_init(hp);
  1651. return 1;
  1652. }
  1653. return 0;
  1654. }
  1655. /* hp->happy_lock must be held */
  1656. static void happy_meal_mif_interrupt(struct happy_meal *hp)
  1657. {
  1658. void __iomem *tregs = hp->tcvregs;
  1659. printk(KERN_INFO "%s: Link status change.\n", hp->dev->name);
  1660. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  1661. hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
  1662. /* Use the fastest transmission protocol possible. */
  1663. if (hp->sw_lpa & LPA_100FULL) {
  1664. printk(KERN_INFO "%s: Switching to 100Mbps at full duplex.", hp->dev->name);
  1665. hp->sw_bmcr |= (BMCR_FULLDPLX | BMCR_SPEED100);
  1666. } else if (hp->sw_lpa & LPA_100HALF) {
  1667. printk(KERN_INFO "%s: Switching to 100MBps at half duplex.", hp->dev->name);
  1668. hp->sw_bmcr |= BMCR_SPEED100;
  1669. } else if (hp->sw_lpa & LPA_10FULL) {
  1670. printk(KERN_INFO "%s: Switching to 10MBps at full duplex.", hp->dev->name);
  1671. hp->sw_bmcr |= BMCR_FULLDPLX;
  1672. } else {
  1673. printk(KERN_INFO "%s: Using 10Mbps at half duplex.", hp->dev->name);
  1674. }
  1675. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1676. /* Finally stop polling and shut up the MIF. */
  1677. happy_meal_poll_stop(hp, tregs);
  1678. }
  1679. #ifdef TXDEBUG
  1680. #define TXD(x) printk x
  1681. #else
  1682. #define TXD(x)
  1683. #endif
  1684. /* hp->happy_lock must be held */
  1685. static void happy_meal_tx(struct happy_meal *hp)
  1686. {
  1687. struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
  1688. struct happy_meal_txd *this;
  1689. struct net_device *dev = hp->dev;
  1690. int elem;
  1691. elem = hp->tx_old;
  1692. TXD(("TX<"));
  1693. while (elem != hp->tx_new) {
  1694. struct sk_buff *skb;
  1695. u32 flags, dma_addr, dma_len;
  1696. int frag;
  1697. TXD(("[%d]", elem));
  1698. this = &txbase[elem];
  1699. flags = hme_read_desc32(hp, &this->tx_flags);
  1700. if (flags & TXFLAG_OWN)
  1701. break;
  1702. skb = hp->tx_skbs[elem];
  1703. if (skb_shinfo(skb)->nr_frags) {
  1704. int last;
  1705. last = elem + skb_shinfo(skb)->nr_frags;
  1706. last &= (TX_RING_SIZE - 1);
  1707. flags = hme_read_desc32(hp, &txbase[last].tx_flags);
  1708. if (flags & TXFLAG_OWN)
  1709. break;
  1710. }
  1711. hp->tx_skbs[elem] = NULL;
  1712. hp->net_stats.tx_bytes += skb->len;
  1713. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  1714. dma_addr = hme_read_desc32(hp, &this->tx_addr);
  1715. dma_len = hme_read_desc32(hp, &this->tx_flags);
  1716. dma_len &= TXFLAG_SIZE;
  1717. hme_dma_unmap(hp, dma_addr, dma_len, DMA_TODEVICE);
  1718. elem = NEXT_TX(elem);
  1719. this = &txbase[elem];
  1720. }
  1721. dev_kfree_skb_irq(skb);
  1722. hp->net_stats.tx_packets++;
  1723. }
  1724. hp->tx_old = elem;
  1725. TXD((">"));
  1726. if (netif_queue_stopped(dev) &&
  1727. TX_BUFFS_AVAIL(hp) > (MAX_SKB_FRAGS + 1))
  1728. netif_wake_queue(dev);
  1729. }
  1730. #ifdef RXDEBUG
  1731. #define RXD(x) printk x
  1732. #else
  1733. #define RXD(x)
  1734. #endif
  1735. /* Originally I used to handle the allocation failure by just giving back just
  1736. * that one ring buffer to the happy meal. Problem is that usually when that
  1737. * condition is triggered, the happy meal expects you to do something reasonable
  1738. * with all of the packets it has DMA'd in. So now I just drop the entire
  1739. * ring when we cannot get a new skb and give them all back to the happy meal,
  1740. * maybe things will be "happier" now.
  1741. *
  1742. * hp->happy_lock must be held
  1743. */
  1744. static void happy_meal_rx(struct happy_meal *hp, struct net_device *dev)
  1745. {
  1746. struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
  1747. struct happy_meal_rxd *this;
  1748. int elem = hp->rx_new, drops = 0;
  1749. u32 flags;
  1750. RXD(("RX<"));
  1751. this = &rxbase[elem];
  1752. while (!((flags = hme_read_desc32(hp, &this->rx_flags)) & RXFLAG_OWN)) {
  1753. struct sk_buff *skb;
  1754. int len = flags >> 16;
  1755. u16 csum = flags & RXFLAG_CSUM;
  1756. u32 dma_addr = hme_read_desc32(hp, &this->rx_addr);
  1757. RXD(("[%d ", elem));
  1758. /* Check for errors. */
  1759. if ((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
  1760. RXD(("ERR(%08x)]", flags));
  1761. hp->net_stats.rx_errors++;
  1762. if (len < ETH_ZLEN)
  1763. hp->net_stats.rx_length_errors++;
  1764. if (len & (RXFLAG_OVERFLOW >> 16)) {
  1765. hp->net_stats.rx_over_errors++;
  1766. hp->net_stats.rx_fifo_errors++;
  1767. }
  1768. /* Return it to the Happy meal. */
  1769. drop_it:
  1770. hp->net_stats.rx_dropped++;
  1771. hme_write_rxd(hp, this,
  1772. (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
  1773. dma_addr);
  1774. goto next;
  1775. }
  1776. skb = hp->rx_skbs[elem];
  1777. if (len > RX_COPY_THRESHOLD) {
  1778. struct sk_buff *new_skb;
  1779. /* Now refill the entry, if we can. */
  1780. new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  1781. if (new_skb == NULL) {
  1782. drops++;
  1783. goto drop_it;
  1784. }
  1785. hme_dma_unmap(hp, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE);
  1786. hp->rx_skbs[elem] = new_skb;
  1787. new_skb->dev = dev;
  1788. skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
  1789. hme_write_rxd(hp, this,
  1790. (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
  1791. hme_dma_map(hp, new_skb->data, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE));
  1792. skb_reserve(new_skb, RX_OFFSET);
  1793. /* Trim the original skb for the netif. */
  1794. skb_trim(skb, len);
  1795. } else {
  1796. struct sk_buff *copy_skb = dev_alloc_skb(len + 2);
  1797. if (copy_skb == NULL) {
  1798. drops++;
  1799. goto drop_it;
  1800. }
  1801. skb_reserve(copy_skb, 2);
  1802. skb_put(copy_skb, len);
  1803. hme_dma_sync_for_cpu(hp, dma_addr, len, DMA_FROMDEVICE);
  1804. skb_copy_from_linear_data(skb, copy_skb->data, len);
  1805. hme_dma_sync_for_device(hp, dma_addr, len, DMA_FROMDEVICE);
  1806. /* Reuse original ring buffer. */
  1807. hme_write_rxd(hp, this,
  1808. (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
  1809. dma_addr);
  1810. skb = copy_skb;
  1811. }
  1812. /* This card is _fucking_ hot... */
  1813. skb->csum = csum_unfold(~(__force __sum16)htons(csum));
  1814. skb->ip_summed = CHECKSUM_COMPLETE;
  1815. RXD(("len=%d csum=%4x]", len, csum));
  1816. skb->protocol = eth_type_trans(skb, dev);
  1817. netif_rx(skb);
  1818. dev->last_rx = jiffies;
  1819. hp->net_stats.rx_packets++;
  1820. hp->net_stats.rx_bytes += len;
  1821. next:
  1822. elem = NEXT_RX(elem);
  1823. this = &rxbase[elem];
  1824. }
  1825. hp->rx_new = elem;
  1826. if (drops)
  1827. printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
  1828. RXD((">"));
  1829. }
  1830. static irqreturn_t happy_meal_interrupt(int irq, void *dev_id)
  1831. {
  1832. struct net_device *dev = dev_id;
  1833. struct happy_meal *hp = netdev_priv(dev);
  1834. u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
  1835. HMD(("happy_meal_interrupt: status=%08x ", happy_status));
  1836. spin_lock(&hp->happy_lock);
  1837. if (happy_status & GREG_STAT_ERRORS) {
  1838. HMD(("ERRORS "));
  1839. if (happy_meal_is_not_so_happy(hp, /* un- */ happy_status))
  1840. goto out;
  1841. }
  1842. if (happy_status & GREG_STAT_MIFIRQ) {
  1843. HMD(("MIFIRQ "));
  1844. happy_meal_mif_interrupt(hp);
  1845. }
  1846. if (happy_status & GREG_STAT_TXALL) {
  1847. HMD(("TXALL "));
  1848. happy_meal_tx(hp);
  1849. }
  1850. if (happy_status & GREG_STAT_RXTOHOST) {
  1851. HMD(("RXTOHOST "));
  1852. happy_meal_rx(hp, dev);
  1853. }
  1854. HMD(("done\n"));
  1855. out:
  1856. spin_unlock(&hp->happy_lock);
  1857. return IRQ_HANDLED;
  1858. }
  1859. #ifdef CONFIG_SBUS
  1860. static irqreturn_t quattro_sbus_interrupt(int irq, void *cookie)
  1861. {
  1862. struct quattro *qp = (struct quattro *) cookie;
  1863. int i;
  1864. for (i = 0; i < 4; i++) {
  1865. struct net_device *dev = qp->happy_meals[i];
  1866. struct happy_meal *hp = dev->priv;
  1867. u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
  1868. HMD(("quattro_interrupt: status=%08x ", happy_status));
  1869. if (!(happy_status & (GREG_STAT_ERRORS |
  1870. GREG_STAT_MIFIRQ |
  1871. GREG_STAT_TXALL |
  1872. GREG_STAT_RXTOHOST)))
  1873. continue;
  1874. spin_lock(&hp->happy_lock);
  1875. if (happy_status & GREG_STAT_ERRORS) {
  1876. HMD(("ERRORS "));
  1877. if (happy_meal_is_not_so_happy(hp, happy_status))
  1878. goto next;
  1879. }
  1880. if (happy_status & GREG_STAT_MIFIRQ) {
  1881. HMD(("MIFIRQ "));
  1882. happy_meal_mif_interrupt(hp);
  1883. }
  1884. if (happy_status & GREG_STAT_TXALL) {
  1885. HMD(("TXALL "));
  1886. happy_meal_tx(hp);
  1887. }
  1888. if (happy_status & GREG_STAT_RXTOHOST) {
  1889. HMD(("RXTOHOST "));
  1890. happy_meal_rx(hp, dev);
  1891. }
  1892. next:
  1893. spin_unlock(&hp->happy_lock);
  1894. }
  1895. HMD(("done\n"));
  1896. return IRQ_HANDLED;
  1897. }
  1898. #endif
  1899. static int happy_meal_open(struct net_device *dev)
  1900. {
  1901. struct happy_meal *hp = dev->priv;
  1902. int res;
  1903. HMD(("happy_meal_open: "));
  1904. /* On SBUS Quattro QFE cards, all hme interrupts are concentrated
  1905. * into a single source which we register handling at probe time.
  1906. */
  1907. if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO) {
  1908. if (request_irq(dev->irq, &happy_meal_interrupt,
  1909. IRQF_SHARED, dev->name, (void *)dev)) {
  1910. HMD(("EAGAIN\n"));
  1911. printk(KERN_ERR "happy_meal(SBUS): Can't order irq %d to go.\n",
  1912. dev->irq);
  1913. return -EAGAIN;
  1914. }
  1915. }
  1916. HMD(("to happy_meal_init\n"));
  1917. spin_lock_irq(&hp->happy_lock);
  1918. res = happy_meal_init(hp);
  1919. spin_unlock_irq(&hp->happy_lock);
  1920. if (res && ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO))
  1921. free_irq(dev->irq, dev);
  1922. return res;
  1923. }
  1924. static int happy_meal_close(struct net_device *dev)
  1925. {
  1926. struct happy_meal *hp = dev->priv;
  1927. spin_lock_irq(&hp->happy_lock);
  1928. happy_meal_stop(hp, hp->gregs);
  1929. happy_meal_clean_rings(hp);
  1930. /* If auto-negotiation timer is running, kill it. */
  1931. del_timer(&hp->happy_timer);
  1932. spin_unlock_irq(&hp->happy_lock);
  1933. /* On Quattro QFE cards, all hme interrupts are concentrated
  1934. * into a single source which we register handling at probe
  1935. * time and never unregister.
  1936. */
  1937. if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO)
  1938. free_irq(dev->irq, dev);
  1939. return 0;
  1940. }
  1941. #ifdef SXDEBUG
  1942. #define SXD(x) printk x
  1943. #else
  1944. #define SXD(x)
  1945. #endif
  1946. static void happy_meal_tx_timeout(struct net_device *dev)
  1947. {
  1948. struct happy_meal *hp = dev->priv;
  1949. printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  1950. tx_dump_log();
  1951. printk (KERN_ERR "%s: Happy Status %08x TX[%08x:%08x]\n", dev->name,
  1952. hme_read32(hp, hp->gregs + GREG_STAT),
  1953. hme_read32(hp, hp->etxregs + ETX_CFG),
  1954. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG));
  1955. spin_lock_irq(&hp->happy_lock);
  1956. happy_meal_init(hp);
  1957. spin_unlock_irq(&hp->happy_lock);
  1958. netif_wake_queue(dev);
  1959. }
  1960. static int happy_meal_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1961. {
  1962. struct happy_meal *hp = dev->priv;
  1963. int entry;
  1964. u32 tx_flags;
  1965. tx_flags = TXFLAG_OWN;
  1966. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1967. const u32 csum_start_off = skb_transport_offset(skb);
  1968. const u32 csum_stuff_off = csum_start_off + skb->csum_offset;
  1969. tx_flags = (TXFLAG_OWN | TXFLAG_CSENABLE |
  1970. ((csum_start_off << 14) & TXFLAG_CSBUFBEGIN) |
  1971. ((csum_stuff_off << 20) & TXFLAG_CSLOCATION));
  1972. }
  1973. spin_lock_irq(&hp->happy_lock);
  1974. if (TX_BUFFS_AVAIL(hp) <= (skb_shinfo(skb)->nr_frags + 1)) {
  1975. netif_stop_queue(dev);
  1976. spin_unlock_irq(&hp->happy_lock);
  1977. printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
  1978. dev->name);
  1979. return 1;
  1980. }
  1981. entry = hp->tx_new;
  1982. SXD(("SX<l[%d]e[%d]>", len, entry));
  1983. hp->tx_skbs[entry] = skb;
  1984. if (skb_shinfo(skb)->nr_frags == 0) {
  1985. u32 mapping, len;
  1986. len = skb->len;
  1987. mapping = hme_dma_map(hp, skb->data, len, DMA_TODEVICE);
  1988. tx_flags |= (TXFLAG_SOP | TXFLAG_EOP);
  1989. hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
  1990. (tx_flags | (len & TXFLAG_SIZE)),
  1991. mapping);
  1992. entry = NEXT_TX(entry);
  1993. } else {
  1994. u32 first_len, first_mapping;
  1995. int frag, first_entry = entry;
  1996. /* We must give this initial chunk to the device last.
  1997. * Otherwise we could race with the device.
  1998. */
  1999. first_len = skb_headlen(skb);
  2000. first_mapping = hme_dma_map(hp, skb->data, first_len, DMA_TODEVICE);
  2001. entry = NEXT_TX(entry);
  2002. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  2003. skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
  2004. u32 len, mapping, this_txflags;
  2005. len = this_frag->size;
  2006. mapping = hme_dma_map(hp,
  2007. ((void *) page_address(this_frag->page) +
  2008. this_frag->page_offset),
  2009. len, DMA_TODEVICE);
  2010. this_txflags = tx_flags;
  2011. if (frag == skb_shinfo(skb)->nr_frags - 1)
  2012. this_txflags |= TXFLAG_EOP;
  2013. hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
  2014. (this_txflags | (len & TXFLAG_SIZE)),
  2015. mapping);
  2016. entry = NEXT_TX(entry);
  2017. }
  2018. hme_write_txd(hp, &hp->happy_block->happy_meal_txd[first_entry],
  2019. (tx_flags | TXFLAG_SOP | (first_len & TXFLAG_SIZE)),
  2020. first_mapping);
  2021. }
  2022. hp->tx_new = entry;
  2023. if (TX_BUFFS_AVAIL(hp) <= (MAX_SKB_FRAGS + 1))
  2024. netif_stop_queue(dev);
  2025. /* Get it going. */
  2026. hme_write32(hp, hp->etxregs + ETX_PENDING, ETX_TP_DMAWAKEUP);
  2027. spin_unlock_irq(&hp->happy_lock);
  2028. dev->trans_start = jiffies;
  2029. tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
  2030. return 0;
  2031. }
  2032. static struct net_device_stats *happy_meal_get_stats(struct net_device *dev)
  2033. {
  2034. struct happy_meal *hp = dev->priv;
  2035. spin_lock_irq(&hp->happy_lock);
  2036. happy_meal_get_counters(hp, hp->bigmacregs);
  2037. spin_unlock_irq(&hp->happy_lock);
  2038. return &hp->net_stats;
  2039. }
  2040. static void happy_meal_set_multicast(struct net_device *dev)
  2041. {
  2042. struct happy_meal *hp = dev->priv;
  2043. void __iomem *bregs = hp->bigmacregs;
  2044. struct dev_mc_list *dmi = dev->mc_list;
  2045. char *addrs;
  2046. int i;
  2047. u32 crc;
  2048. spin_lock_irq(&hp->happy_lock);
  2049. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  2050. hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
  2051. hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
  2052. hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
  2053. hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
  2054. } else if (dev->flags & IFF_PROMISC) {
  2055. hme_write32(hp, bregs + BMAC_RXCFG,
  2056. hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_PMISC);
  2057. } else {
  2058. u16 hash_table[4];
  2059. for (i = 0; i < 4; i++)
  2060. hash_table[i] = 0;
  2061. for (i = 0; i < dev->mc_count; i++) {
  2062. addrs = dmi->dmi_addr;
  2063. dmi = dmi->next;
  2064. if (!(*addrs & 1))
  2065. continue;
  2066. crc = ether_crc_le(6, addrs);
  2067. crc >>= 26;
  2068. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  2069. }
  2070. hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
  2071. hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
  2072. hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
  2073. hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
  2074. }
  2075. spin_unlock_irq(&hp->happy_lock);
  2076. }
  2077. /* Ethtool support... */
  2078. static int hme_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2079. {
  2080. struct happy_meal *hp = dev->priv;
  2081. cmd->supported =
  2082. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  2083. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  2084. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
  2085. /* XXX hardcoded stuff for now */
  2086. cmd->port = PORT_TP; /* XXX no MII support */
  2087. cmd->transceiver = XCVR_INTERNAL; /* XXX no external xcvr support */
  2088. cmd->phy_address = 0; /* XXX fixed PHYAD */
  2089. /* Record PHY settings. */
  2090. spin_lock_irq(&hp->happy_lock);
  2091. hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
  2092. hp->sw_lpa = happy_meal_tcvr_read(hp, hp->tcvregs, MII_LPA);
  2093. spin_unlock_irq(&hp->happy_lock);
  2094. if (hp->sw_bmcr & BMCR_ANENABLE) {
  2095. cmd->autoneg = AUTONEG_ENABLE;
  2096. cmd->speed =
  2097. (hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) ?
  2098. SPEED_100 : SPEED_10;
  2099. if (cmd->speed == SPEED_100)
  2100. cmd->duplex =
  2101. (hp->sw_lpa & (LPA_100FULL)) ?
  2102. DUPLEX_FULL : DUPLEX_HALF;
  2103. else
  2104. cmd->duplex =
  2105. (hp->sw_lpa & (LPA_10FULL)) ?
  2106. DUPLEX_FULL : DUPLEX_HALF;
  2107. } else {
  2108. cmd->autoneg = AUTONEG_DISABLE;
  2109. cmd->speed =
  2110. (hp->sw_bmcr & BMCR_SPEED100) ?
  2111. SPEED_100 : SPEED_10;
  2112. cmd->duplex =
  2113. (hp->sw_bmcr & BMCR_FULLDPLX) ?
  2114. DUPLEX_FULL : DUPLEX_HALF;
  2115. }
  2116. return 0;
  2117. }
  2118. static int hme_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2119. {
  2120. struct happy_meal *hp = dev->priv;
  2121. /* Verify the settings we care about. */
  2122. if (cmd->autoneg != AUTONEG_ENABLE &&
  2123. cmd->autoneg != AUTONEG_DISABLE)
  2124. return -EINVAL;
  2125. if (cmd->autoneg == AUTONEG_DISABLE &&
  2126. ((cmd->speed != SPEED_100 &&
  2127. cmd->speed != SPEED_10) ||
  2128. (cmd->duplex != DUPLEX_HALF &&
  2129. cmd->duplex != DUPLEX_FULL)))
  2130. return -EINVAL;
  2131. /* Ok, do it to it. */
  2132. spin_lock_irq(&hp->happy_lock);
  2133. del_timer(&hp->happy_timer);
  2134. happy_meal_begin_auto_negotiation(hp, hp->tcvregs, cmd);
  2135. spin_unlock_irq(&hp->happy_lock);
  2136. return 0;
  2137. }
  2138. static void hme_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  2139. {
  2140. struct happy_meal *hp = dev->priv;
  2141. strcpy(info->driver, "sunhme");
  2142. strcpy(info->version, "2.02");
  2143. if (hp->happy_flags & HFLAG_PCI) {
  2144. struct pci_dev *pdev = hp->happy_dev;
  2145. strcpy(info->bus_info, pci_name(pdev));
  2146. }
  2147. #ifdef CONFIG_SBUS
  2148. else {
  2149. struct sbus_dev *sdev = hp->happy_dev;
  2150. sprintf(info->bus_info, "SBUS:%d",
  2151. sdev->slot);
  2152. }
  2153. #endif
  2154. }
  2155. static u32 hme_get_link(struct net_device *dev)
  2156. {
  2157. struct happy_meal *hp = dev->priv;
  2158. spin_lock_irq(&hp->happy_lock);
  2159. hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
  2160. spin_unlock_irq(&hp->happy_lock);
  2161. return (hp->sw_bmsr & BMSR_LSTATUS);
  2162. }
  2163. static const struct ethtool_ops hme_ethtool_ops = {
  2164. .get_settings = hme_get_settings,
  2165. .set_settings = hme_set_settings,
  2166. .get_drvinfo = hme_get_drvinfo,
  2167. .get_link = hme_get_link,
  2168. };
  2169. static int hme_version_printed;
  2170. #ifdef CONFIG_SBUS
  2171. void __devinit quattro_get_ranges(struct quattro *qp)
  2172. {
  2173. struct sbus_dev *sdev = qp->quattro_dev;
  2174. int err;
  2175. err = prom_getproperty(sdev->prom_node,
  2176. "ranges",
  2177. (char *)&qp->ranges[0],
  2178. sizeof(qp->ranges));
  2179. if (err == 0 || err == -1) {
  2180. qp->nranges = 0;
  2181. return;
  2182. }
  2183. qp->nranges = (err / sizeof(struct linux_prom_ranges));
  2184. }
  2185. static void __devinit quattro_apply_ranges(struct quattro *qp, struct happy_meal *hp)
  2186. {
  2187. struct sbus_dev *sdev = hp->happy_dev;
  2188. int rng;
  2189. for (rng = 0; rng < qp->nranges; rng++) {
  2190. struct linux_prom_ranges *rngp = &qp->ranges[rng];
  2191. int reg;
  2192. for (reg = 0; reg < 5; reg++) {
  2193. if (sdev->reg_addrs[reg].which_io ==
  2194. rngp->ot_child_space)
  2195. break;
  2196. }
  2197. if (reg == 5)
  2198. continue;
  2199. sdev->reg_addrs[reg].which_io = rngp->ot_parent_space;
  2200. sdev->reg_addrs[reg].phys_addr += rngp->ot_parent_base;
  2201. }
  2202. }
  2203. /* Given a happy meal sbus device, find it's quattro parent.
  2204. * If none exist, allocate and return a new one.
  2205. *
  2206. * Return NULL on failure.
  2207. */
  2208. static struct quattro * __devinit quattro_sbus_find(struct sbus_dev *goal_sdev)
  2209. {
  2210. struct sbus_dev *sdev;
  2211. struct quattro *qp;
  2212. int i;
  2213. for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
  2214. for (i = 0, sdev = qp->quattro_dev;
  2215. (sdev != NULL) && (i < 4);
  2216. sdev = sdev->next, i++) {
  2217. if (sdev == goal_sdev)
  2218. return qp;
  2219. }
  2220. }
  2221. qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
  2222. if (qp != NULL) {
  2223. int i;
  2224. for (i = 0; i < 4; i++)
  2225. qp->happy_meals[i] = NULL;
  2226. qp->quattro_dev = goal_sdev;
  2227. qp->next = qfe_sbus_list;
  2228. qfe_sbus_list = qp;
  2229. quattro_get_ranges(qp);
  2230. }
  2231. return qp;
  2232. }
  2233. /* After all quattro cards have been probed, we call these functions
  2234. * to register the IRQ handlers.
  2235. */
  2236. static void __init quattro_sbus_register_irqs(void)
  2237. {
  2238. struct quattro *qp;
  2239. for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
  2240. struct sbus_dev *sdev = qp->quattro_dev;
  2241. int err;
  2242. err = request_irq(sdev->irqs[0],
  2243. quattro_sbus_interrupt,
  2244. IRQF_SHARED, "Quattro",
  2245. qp);
  2246. if (err != 0) {
  2247. printk(KERN_ERR "Quattro: Fatal IRQ registery error %d.\n", err);
  2248. panic("QFE request irq");
  2249. }
  2250. }
  2251. }
  2252. static void quattro_sbus_free_irqs(void)
  2253. {
  2254. struct quattro *qp;
  2255. for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
  2256. struct sbus_dev *sdev = qp->quattro_dev;
  2257. free_irq(sdev->irqs[0], qp);
  2258. }
  2259. }
  2260. #endif /* CONFIG_SBUS */
  2261. #ifdef CONFIG_PCI
  2262. static struct quattro * __devinit quattro_pci_find(struct pci_dev *pdev)
  2263. {
  2264. struct pci_dev *bdev = pdev->bus->self;
  2265. struct quattro *qp;
  2266. if (!bdev) return NULL;
  2267. for (qp = qfe_pci_list; qp != NULL; qp = qp->next) {
  2268. struct pci_dev *qpdev = qp->quattro_dev;
  2269. if (qpdev == bdev)
  2270. return qp;
  2271. }
  2272. qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
  2273. if (qp != NULL) {
  2274. int i;
  2275. for (i = 0; i < 4; i++)
  2276. qp->happy_meals[i] = NULL;
  2277. qp->quattro_dev = bdev;
  2278. qp->next = qfe_pci_list;
  2279. qfe_pci_list = qp;
  2280. /* No range tricks necessary on PCI. */
  2281. qp->nranges = 0;
  2282. }
  2283. return qp;
  2284. }
  2285. #endif /* CONFIG_PCI */
  2286. #ifdef CONFIG_SBUS
  2287. static int __devinit happy_meal_sbus_probe_one(struct sbus_dev *sdev, int is_qfe)
  2288. {
  2289. struct device_node *dp = sdev->ofdev.node;
  2290. struct quattro *qp = NULL;
  2291. struct happy_meal *hp;
  2292. struct net_device *dev;
  2293. int i, qfe_slot = -1;
  2294. int err = -ENODEV;
  2295. DECLARE_MAC_BUF(mac);
  2296. if (is_qfe) {
  2297. qp = quattro_sbus_find(sdev);
  2298. if (qp == NULL)
  2299. goto err_out;
  2300. for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
  2301. if (qp->happy_meals[qfe_slot] == NULL)
  2302. break;
  2303. if (qfe_slot == 4)
  2304. goto err_out;
  2305. }
  2306. err = -ENOMEM;
  2307. dev = alloc_etherdev(sizeof(struct happy_meal));
  2308. if (!dev)
  2309. goto err_out;
  2310. SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
  2311. if (hme_version_printed++ == 0)
  2312. printk(KERN_INFO "%s", version);
  2313. /* If user did not specify a MAC address specifically, use
  2314. * the Quattro local-mac-address property...
  2315. */
  2316. for (i = 0; i < 6; i++) {
  2317. if (macaddr[i] != 0)
  2318. break;
  2319. }
  2320. if (i < 6) { /* a mac address was given */
  2321. for (i = 0; i < 6; i++)
  2322. dev->dev_addr[i] = macaddr[i];
  2323. macaddr[5]++;
  2324. } else {
  2325. const unsigned char *addr;
  2326. int len;
  2327. addr = of_get_property(dp, "local-mac-address", &len);
  2328. if (qfe_slot != -1 && addr && len == 6)
  2329. memcpy(dev->dev_addr, addr, 6);
  2330. else
  2331. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  2332. }
  2333. hp = dev->priv;
  2334. hp->happy_dev = sdev;
  2335. spin_lock_init(&hp->happy_lock);
  2336. err = -ENODEV;
  2337. if (sdev->num_registers != 5) {
  2338. printk(KERN_ERR "happymeal: Device needs 5 regs, has %d.\n",
  2339. sdev->num_registers);
  2340. goto err_out_free_netdev;
  2341. }
  2342. if (qp != NULL) {
  2343. hp->qfe_parent = qp;
  2344. hp->qfe_ent = qfe_slot;
  2345. qp->happy_meals[qfe_slot] = dev;
  2346. quattro_apply_ranges(qp, hp);
  2347. }
  2348. hp->gregs = sbus_ioremap(&sdev->resource[0], 0,
  2349. GREG_REG_SIZE, "HME Global Regs");
  2350. if (!hp->gregs) {
  2351. printk(KERN_ERR "happymeal: Cannot map global registers.\n");
  2352. goto err_out_free_netdev;
  2353. }
  2354. hp->etxregs = sbus_ioremap(&sdev->resource[1], 0,
  2355. ETX_REG_SIZE, "HME TX Regs");
  2356. if (!hp->etxregs) {
  2357. printk(KERN_ERR "happymeal: Cannot map MAC TX registers.\n");
  2358. goto err_out_iounmap;
  2359. }
  2360. hp->erxregs = sbus_ioremap(&sdev->resource[2], 0,
  2361. ERX_REG_SIZE, "HME RX Regs");
  2362. if (!hp->erxregs) {
  2363. printk(KERN_ERR "happymeal: Cannot map MAC RX registers.\n");
  2364. goto err_out_iounmap;
  2365. }
  2366. hp->bigmacregs = sbus_ioremap(&sdev->resource[3], 0,
  2367. BMAC_REG_SIZE, "HME BIGMAC Regs");
  2368. if (!hp->bigmacregs) {
  2369. printk(KERN_ERR "happymeal: Cannot map BIGMAC registers.\n");
  2370. goto err_out_iounmap;
  2371. }
  2372. hp->tcvregs = sbus_ioremap(&sdev->resource[4], 0,
  2373. TCVR_REG_SIZE, "HME Tranceiver Regs");
  2374. if (!hp->tcvregs) {
  2375. printk(KERN_ERR "happymeal: Cannot map TCVR registers.\n");
  2376. goto err_out_iounmap;
  2377. }
  2378. hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
  2379. if (hp->hm_revision == 0xff)
  2380. hp->hm_revision = 0xa0;
  2381. /* Now enable the feature flags we can. */
  2382. if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
  2383. hp->happy_flags = HFLAG_20_21;
  2384. else if (hp->hm_revision != 0xa0)
  2385. hp->happy_flags = HFLAG_NOT_A0;
  2386. if (qp != NULL)
  2387. hp->happy_flags |= HFLAG_QUATTRO;
  2388. /* Get the supported DVMA burst sizes from our Happy SBUS. */
  2389. hp->happy_bursts = of_getintprop_default(sdev->bus->ofdev.node,
  2390. "burst-sizes", 0x00);
  2391. hp->happy_block = sbus_alloc_consistent(hp->happy_dev,
  2392. PAGE_SIZE,
  2393. &hp->hblock_dvma);
  2394. err = -ENOMEM;
  2395. if (!hp->happy_block) {
  2396. printk(KERN_ERR "happymeal: Cannot allocate descriptors.\n");
  2397. goto err_out_iounmap;
  2398. }
  2399. /* Force check of the link first time we are brought up. */
  2400. hp->linkcheck = 0;
  2401. /* Force timer state to 'asleep' with count of zero. */
  2402. hp->timer_state = asleep;
  2403. hp->timer_ticks = 0;
  2404. init_timer(&hp->happy_timer);
  2405. hp->dev = dev;
  2406. dev->open = &happy_meal_open;
  2407. dev->stop = &happy_meal_close;
  2408. dev->hard_start_xmit = &happy_meal_start_xmit;
  2409. dev->get_stats = &happy_meal_get_stats;
  2410. dev->set_multicast_list = &happy_meal_set_multicast;
  2411. dev->tx_timeout = &happy_meal_tx_timeout;
  2412. dev->watchdog_timeo = 5*HZ;
  2413. dev->ethtool_ops = &hme_ethtool_ops;
  2414. /* Happy Meal can do it all... */
  2415. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2416. dev->irq = sdev->irqs[0];
  2417. #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
  2418. /* Hook up PCI register/dma accessors. */
  2419. hp->read_desc32 = sbus_hme_read_desc32;
  2420. hp->write_txd = sbus_hme_write_txd;
  2421. hp->write_rxd = sbus_hme_write_rxd;
  2422. hp->dma_map = (u32 (*)(void *, void *, long, int))sbus_map_single;
  2423. hp->dma_unmap = (void (*)(void *, u32, long, int))sbus_unmap_single;
  2424. hp->dma_sync_for_cpu = (void (*)(void *, u32, long, int))
  2425. sbus_dma_sync_single_for_cpu;
  2426. hp->dma_sync_for_device = (void (*)(void *, u32, long, int))
  2427. sbus_dma_sync_single_for_device;
  2428. hp->read32 = sbus_hme_read32;
  2429. hp->write32 = sbus_hme_write32;
  2430. #endif
  2431. /* Grrr, Happy Meal comes up by default not advertising
  2432. * full duplex 100baseT capabilities, fix this.
  2433. */
  2434. spin_lock_irq(&hp->happy_lock);
  2435. happy_meal_set_initial_advertisement(hp);
  2436. spin_unlock_irq(&hp->happy_lock);
  2437. if (register_netdev(hp->dev)) {
  2438. printk(KERN_ERR "happymeal: Cannot register net device, "
  2439. "aborting.\n");
  2440. goto err_out_free_consistent;
  2441. }
  2442. dev_set_drvdata(&sdev->ofdev.dev, hp);
  2443. if (qfe_slot != -1)
  2444. printk(KERN_INFO "%s: Quattro HME slot %d (SBUS) 10/100baseT Ethernet ",
  2445. dev->name, qfe_slot);
  2446. else
  2447. printk(KERN_INFO "%s: HAPPY MEAL (SBUS) 10/100baseT Ethernet ",
  2448. dev->name);
  2449. printk("%s\n", print_mac(mac, dev->dev_addr));
  2450. return 0;
  2451. err_out_free_consistent:
  2452. sbus_free_consistent(hp->happy_dev,
  2453. PAGE_SIZE,
  2454. hp->happy_block,
  2455. hp->hblock_dvma);
  2456. err_out_iounmap:
  2457. if (hp->gregs)
  2458. sbus_iounmap(hp->gregs, GREG_REG_SIZE);
  2459. if (hp->etxregs)
  2460. sbus_iounmap(hp->etxregs, ETX_REG_SIZE);
  2461. if (hp->erxregs)
  2462. sbus_iounmap(hp->erxregs, ERX_REG_SIZE);
  2463. if (hp->bigmacregs)
  2464. sbus_iounmap(hp->bigmacregs, BMAC_REG_SIZE);
  2465. if (hp->tcvregs)
  2466. sbus_iounmap(hp->tcvregs, TCVR_REG_SIZE);
  2467. err_out_free_netdev:
  2468. free_netdev(dev);
  2469. err_out:
  2470. return err;
  2471. }
  2472. #endif
  2473. #ifdef CONFIG_PCI
  2474. #ifndef CONFIG_SPARC
  2475. static int is_quattro_p(struct pci_dev *pdev)
  2476. {
  2477. struct pci_dev *busdev = pdev->bus->self;
  2478. struct list_head *tmp;
  2479. int n_hmes;
  2480. if (busdev == NULL ||
  2481. busdev->vendor != PCI_VENDOR_ID_DEC ||
  2482. busdev->device != PCI_DEVICE_ID_DEC_21153)
  2483. return 0;
  2484. n_hmes = 0;
  2485. tmp = pdev->bus->devices.next;
  2486. while (tmp != &pdev->bus->devices) {
  2487. struct pci_dev *this_pdev = pci_dev_b(tmp);
  2488. if (this_pdev->vendor == PCI_VENDOR_ID_SUN &&
  2489. this_pdev->device == PCI_DEVICE_ID_SUN_HAPPYMEAL)
  2490. n_hmes++;
  2491. tmp = tmp->next;
  2492. }
  2493. if (n_hmes != 4)
  2494. return 0;
  2495. return 1;
  2496. }
  2497. /* Fetch MAC address from vital product data of PCI ROM. */
  2498. static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, int index, unsigned char *dev_addr)
  2499. {
  2500. int this_offset;
  2501. for (this_offset = 0x20; this_offset < len; this_offset++) {
  2502. void __iomem *p = rom_base + this_offset;
  2503. if (readb(p + 0) != 0x90 ||
  2504. readb(p + 1) != 0x00 ||
  2505. readb(p + 2) != 0x09 ||
  2506. readb(p + 3) != 0x4e ||
  2507. readb(p + 4) != 0x41 ||
  2508. readb(p + 5) != 0x06)
  2509. continue;
  2510. this_offset += 6;
  2511. p += 6;
  2512. if (index == 0) {
  2513. int i;
  2514. for (i = 0; i < 6; i++)
  2515. dev_addr[i] = readb(p + i);
  2516. return 1;
  2517. }
  2518. index--;
  2519. }
  2520. return 0;
  2521. }
  2522. static void get_hme_mac_nonsparc(struct pci_dev *pdev, unsigned char *dev_addr)
  2523. {
  2524. size_t size;
  2525. void __iomem *p = pci_map_rom(pdev, &size);
  2526. if (p) {
  2527. int index = 0;
  2528. int found;
  2529. if (is_quattro_p(pdev))
  2530. index = PCI_SLOT(pdev->devfn);
  2531. found = readb(p) == 0x55 &&
  2532. readb(p + 1) == 0xaa &&
  2533. find_eth_addr_in_vpd(p, (64 * 1024), index, dev_addr);
  2534. pci_unmap_rom(pdev, p);
  2535. if (found)
  2536. return;
  2537. }
  2538. /* Sun MAC prefix then 3 random bytes. */
  2539. dev_addr[0] = 0x08;
  2540. dev_addr[1] = 0x00;
  2541. dev_addr[2] = 0x20;
  2542. get_random_bytes(&dev_addr[3], 3);
  2543. return;
  2544. }
  2545. #endif /* !(CONFIG_SPARC) */
  2546. static int __devinit happy_meal_pci_probe(struct pci_dev *pdev,
  2547. const struct pci_device_id *ent)
  2548. {
  2549. struct quattro *qp = NULL;
  2550. #ifdef CONFIG_SPARC
  2551. struct device_node *dp;
  2552. #endif
  2553. struct happy_meal *hp;
  2554. struct net_device *dev;
  2555. void __iomem *hpreg_base;
  2556. unsigned long hpreg_res;
  2557. int i, qfe_slot = -1;
  2558. char prom_name[64];
  2559. int err;
  2560. DECLARE_MAC_BUF(mac);
  2561. /* Now make sure pci_dev cookie is there. */
  2562. #ifdef CONFIG_SPARC
  2563. dp = pci_device_to_OF_node(pdev);
  2564. strcpy(prom_name, dp->name);
  2565. #else
  2566. if (is_quattro_p(pdev))
  2567. strcpy(prom_name, "SUNW,qfe");
  2568. else
  2569. strcpy(prom_name, "SUNW,hme");
  2570. #endif
  2571. err = -ENODEV;
  2572. if (pci_enable_device(pdev))
  2573. goto err_out;
  2574. pci_set_master(pdev);
  2575. if (!strcmp(prom_name, "SUNW,qfe") || !strcmp(prom_name, "qfe")) {
  2576. qp = quattro_pci_find(pdev);
  2577. if (qp == NULL)
  2578. goto err_out;
  2579. for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
  2580. if (qp->happy_meals[qfe_slot] == NULL)
  2581. break;
  2582. if (qfe_slot == 4)
  2583. goto err_out;
  2584. }
  2585. dev = alloc_etherdev(sizeof(struct happy_meal));
  2586. err = -ENOMEM;
  2587. if (!dev)
  2588. goto err_out;
  2589. SET_NETDEV_DEV(dev, &pdev->dev);
  2590. if (hme_version_printed++ == 0)
  2591. printk(KERN_INFO "%s", version);
  2592. dev->base_addr = (long) pdev;
  2593. hp = (struct happy_meal *)dev->priv;
  2594. memset(hp, 0, sizeof(*hp));
  2595. hp->happy_dev = pdev;
  2596. spin_lock_init(&hp->happy_lock);
  2597. if (qp != NULL) {
  2598. hp->qfe_parent = qp;
  2599. hp->qfe_ent = qfe_slot;
  2600. qp->happy_meals[qfe_slot] = dev;
  2601. }
  2602. hpreg_res = pci_resource_start(pdev, 0);
  2603. err = -ENODEV;
  2604. if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
  2605. printk(KERN_ERR "happymeal(PCI): Cannot find proper PCI device base address.\n");
  2606. goto err_out_clear_quattro;
  2607. }
  2608. if (pci_request_regions(pdev, DRV_NAME)) {
  2609. printk(KERN_ERR "happymeal(PCI): Cannot obtain PCI resources, "
  2610. "aborting.\n");
  2611. goto err_out_clear_quattro;
  2612. }
  2613. if ((hpreg_base = ioremap(hpreg_res, 0x8000)) == NULL) {
  2614. printk(KERN_ERR "happymeal(PCI): Unable to remap card memory.\n");
  2615. goto err_out_free_res;
  2616. }
  2617. for (i = 0; i < 6; i++) {
  2618. if (macaddr[i] != 0)
  2619. break;
  2620. }
  2621. if (i < 6) { /* a mac address was given */
  2622. for (i = 0; i < 6; i++)
  2623. dev->dev_addr[i] = macaddr[i];
  2624. macaddr[5]++;
  2625. } else {
  2626. #ifdef CONFIG_SPARC
  2627. const unsigned char *addr;
  2628. int len;
  2629. if (qfe_slot != -1 &&
  2630. (addr = of_get_property(dp,
  2631. "local-mac-address", &len)) != NULL
  2632. && len == 6) {
  2633. memcpy(dev->dev_addr, addr, 6);
  2634. } else {
  2635. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  2636. }
  2637. #else
  2638. get_hme_mac_nonsparc(pdev, &dev->dev_addr[0]);
  2639. #endif
  2640. }
  2641. /* Layout registers. */
  2642. hp->gregs = (hpreg_base + 0x0000UL);
  2643. hp->etxregs = (hpreg_base + 0x2000UL);
  2644. hp->erxregs = (hpreg_base + 0x4000UL);
  2645. hp->bigmacregs = (hpreg_base + 0x6000UL);
  2646. hp->tcvregs = (hpreg_base + 0x7000UL);
  2647. #ifdef CONFIG_SPARC
  2648. hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
  2649. if (hp->hm_revision == 0xff)
  2650. hp->hm_revision = 0xc0 | (pdev->revision & 0x0f);
  2651. #else
  2652. /* works with this on non-sparc hosts */
  2653. hp->hm_revision = 0x20;
  2654. #endif
  2655. /* Now enable the feature flags we can. */
  2656. if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
  2657. hp->happy_flags = HFLAG_20_21;
  2658. else if (hp->hm_revision != 0xa0 && hp->hm_revision != 0xc0)
  2659. hp->happy_flags = HFLAG_NOT_A0;
  2660. if (qp != NULL)
  2661. hp->happy_flags |= HFLAG_QUATTRO;
  2662. /* And of course, indicate this is PCI. */
  2663. hp->happy_flags |= HFLAG_PCI;
  2664. #ifdef CONFIG_SPARC
  2665. /* Assume PCI happy meals can handle all burst sizes. */
  2666. hp->happy_bursts = DMA_BURSTBITS;
  2667. #endif
  2668. hp->happy_block = (struct hmeal_init_block *)
  2669. pci_alloc_consistent(pdev, PAGE_SIZE, &hp->hblock_dvma);
  2670. err = -ENODEV;
  2671. if (!hp->happy_block) {
  2672. printk(KERN_ERR "happymeal(PCI): Cannot get hme init block.\n");
  2673. goto err_out_iounmap;
  2674. }
  2675. hp->linkcheck = 0;
  2676. hp->timer_state = asleep;
  2677. hp->timer_ticks = 0;
  2678. init_timer(&hp->happy_timer);
  2679. hp->dev = dev;
  2680. dev->open = &happy_meal_open;
  2681. dev->stop = &happy_meal_close;
  2682. dev->hard_start_xmit = &happy_meal_start_xmit;
  2683. dev->get_stats = &happy_meal_get_stats;
  2684. dev->set_multicast_list = &happy_meal_set_multicast;
  2685. dev->tx_timeout = &happy_meal_tx_timeout;
  2686. dev->watchdog_timeo = 5*HZ;
  2687. dev->ethtool_ops = &hme_ethtool_ops;
  2688. dev->irq = pdev->irq;
  2689. dev->dma = 0;
  2690. /* Happy Meal can do it all... */
  2691. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2692. #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
  2693. /* Hook up PCI register/dma accessors. */
  2694. hp->read_desc32 = pci_hme_read_desc32;
  2695. hp->write_txd = pci_hme_write_txd;
  2696. hp->write_rxd = pci_hme_write_rxd;
  2697. hp->dma_map = (u32 (*)(void *, void *, long, int))pci_map_single;
  2698. hp->dma_unmap = (void (*)(void *, u32, long, int))pci_unmap_single;
  2699. hp->dma_sync_for_cpu = (void (*)(void *, u32, long, int))
  2700. pci_dma_sync_single_for_cpu;
  2701. hp->dma_sync_for_device = (void (*)(void *, u32, long, int))
  2702. pci_dma_sync_single_for_device;
  2703. hp->read32 = pci_hme_read32;
  2704. hp->write32 = pci_hme_write32;
  2705. #endif
  2706. /* Grrr, Happy Meal comes up by default not advertising
  2707. * full duplex 100baseT capabilities, fix this.
  2708. */
  2709. spin_lock_irq(&hp->happy_lock);
  2710. happy_meal_set_initial_advertisement(hp);
  2711. spin_unlock_irq(&hp->happy_lock);
  2712. if (register_netdev(hp->dev)) {
  2713. printk(KERN_ERR "happymeal(PCI): Cannot register net device, "
  2714. "aborting.\n");
  2715. goto err_out_iounmap;
  2716. }
  2717. dev_set_drvdata(&pdev->dev, hp);
  2718. if (!qfe_slot) {
  2719. struct pci_dev *qpdev = qp->quattro_dev;
  2720. prom_name[0] = 0;
  2721. if (!strncmp(dev->name, "eth", 3)) {
  2722. int i = simple_strtoul(dev->name + 3, NULL, 10);
  2723. sprintf(prom_name, "-%d", i + 3);
  2724. }
  2725. printk(KERN_INFO "%s%s: Quattro HME (PCI/CheerIO) 10/100baseT Ethernet ", dev->name, prom_name);
  2726. if (qpdev->vendor == PCI_VENDOR_ID_DEC &&
  2727. qpdev->device == PCI_DEVICE_ID_DEC_21153)
  2728. printk("DEC 21153 PCI Bridge\n");
  2729. else
  2730. printk("unknown bridge %04x.%04x\n",
  2731. qpdev->vendor, qpdev->device);
  2732. }
  2733. if (qfe_slot != -1)
  2734. printk(KERN_INFO "%s: Quattro HME slot %d (PCI/CheerIO) 10/100baseT Ethernet ",
  2735. dev->name, qfe_slot);
  2736. else
  2737. printk(KERN_INFO "%s: HAPPY MEAL (PCI/CheerIO) 10/100BaseT Ethernet ",
  2738. dev->name);
  2739. printk("%s\n", print_mac(mac, dev->dev_addr));
  2740. return 0;
  2741. err_out_iounmap:
  2742. iounmap(hp->gregs);
  2743. err_out_free_res:
  2744. pci_release_regions(pdev);
  2745. err_out_clear_quattro:
  2746. if (qp != NULL)
  2747. qp->happy_meals[qfe_slot] = NULL;
  2748. free_netdev(dev);
  2749. err_out:
  2750. return err;
  2751. }
  2752. static void __devexit happy_meal_pci_remove(struct pci_dev *pdev)
  2753. {
  2754. struct happy_meal *hp = dev_get_drvdata(&pdev->dev);
  2755. struct net_device *net_dev = hp->dev;
  2756. unregister_netdev(net_dev);
  2757. pci_free_consistent(hp->happy_dev,
  2758. PAGE_SIZE,
  2759. hp->happy_block,
  2760. hp->hblock_dvma);
  2761. iounmap(hp->gregs);
  2762. pci_release_regions(hp->happy_dev);
  2763. free_netdev(net_dev);
  2764. dev_set_drvdata(&pdev->dev, NULL);
  2765. }
  2766. static struct pci_device_id happymeal_pci_ids[] = {
  2767. { PCI_DEVICE(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_HAPPYMEAL) },
  2768. { } /* Terminating entry */
  2769. };
  2770. MODULE_DEVICE_TABLE(pci, happymeal_pci_ids);
  2771. static struct pci_driver hme_pci_driver = {
  2772. .name = "hme",
  2773. .id_table = happymeal_pci_ids,
  2774. .probe = happy_meal_pci_probe,
  2775. .remove = __devexit_p(happy_meal_pci_remove),
  2776. };
  2777. static int __init happy_meal_pci_init(void)
  2778. {
  2779. return pci_register_driver(&hme_pci_driver);
  2780. }
  2781. static void happy_meal_pci_exit(void)
  2782. {
  2783. pci_unregister_driver(&hme_pci_driver);
  2784. while (qfe_pci_list) {
  2785. struct quattro *qfe = qfe_pci_list;
  2786. struct quattro *next = qfe->next;
  2787. kfree(qfe);
  2788. qfe_pci_list = next;
  2789. }
  2790. }
  2791. #endif
  2792. #ifdef CONFIG_SBUS
  2793. static int __devinit hme_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  2794. {
  2795. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  2796. struct device_node *dp = dev->node;
  2797. const char *model = of_get_property(dp, "model", NULL);
  2798. int is_qfe = (match->data != NULL);
  2799. if (!is_qfe && model && !strcmp(model, "SUNW,sbus-qfe"))
  2800. is_qfe = 1;
  2801. return happy_meal_sbus_probe_one(sdev, is_qfe);
  2802. }
  2803. static int __devexit hme_sbus_remove(struct of_device *dev)
  2804. {
  2805. struct happy_meal *hp = dev_get_drvdata(&dev->dev);
  2806. struct net_device *net_dev = hp->dev;
  2807. unregister_netdev(net_dev);
  2808. /* XXX qfe parent interrupt... */
  2809. sbus_iounmap(hp->gregs, GREG_REG_SIZE);
  2810. sbus_iounmap(hp->etxregs, ETX_REG_SIZE);
  2811. sbus_iounmap(hp->erxregs, ERX_REG_SIZE);
  2812. sbus_iounmap(hp->bigmacregs, BMAC_REG_SIZE);
  2813. sbus_iounmap(hp->tcvregs, TCVR_REG_SIZE);
  2814. sbus_free_consistent(hp->happy_dev,
  2815. PAGE_SIZE,
  2816. hp->happy_block,
  2817. hp->hblock_dvma);
  2818. free_netdev(net_dev);
  2819. dev_set_drvdata(&dev->dev, NULL);
  2820. return 0;
  2821. }
  2822. static struct of_device_id hme_sbus_match[] = {
  2823. {
  2824. .name = "SUNW,hme",
  2825. },
  2826. {
  2827. .name = "SUNW,qfe",
  2828. .data = (void *) 1,
  2829. },
  2830. {
  2831. .name = "qfe",
  2832. .data = (void *) 1,
  2833. },
  2834. {},
  2835. };
  2836. MODULE_DEVICE_TABLE(of, hme_sbus_match);
  2837. static struct of_platform_driver hme_sbus_driver = {
  2838. .name = "hme",
  2839. .match_table = hme_sbus_match,
  2840. .probe = hme_sbus_probe,
  2841. .remove = __devexit_p(hme_sbus_remove),
  2842. };
  2843. static int __init happy_meal_sbus_init(void)
  2844. {
  2845. int err;
  2846. err = of_register_driver(&hme_sbus_driver, &sbus_bus_type);
  2847. if (!err)
  2848. quattro_sbus_register_irqs();
  2849. return err;
  2850. }
  2851. static void happy_meal_sbus_exit(void)
  2852. {
  2853. of_unregister_driver(&hme_sbus_driver);
  2854. quattro_sbus_free_irqs();
  2855. while (qfe_sbus_list) {
  2856. struct quattro *qfe = qfe_sbus_list;
  2857. struct quattro *next = qfe->next;
  2858. kfree(qfe);
  2859. qfe_sbus_list = next;
  2860. }
  2861. }
  2862. #endif
  2863. static int __init happy_meal_probe(void)
  2864. {
  2865. int err = 0;
  2866. #ifdef CONFIG_SBUS
  2867. err = happy_meal_sbus_init();
  2868. #endif
  2869. #ifdef CONFIG_PCI
  2870. if (!err) {
  2871. err = happy_meal_pci_init();
  2872. #ifdef CONFIG_SBUS
  2873. if (err)
  2874. happy_meal_sbus_exit();
  2875. #endif
  2876. }
  2877. #endif
  2878. return err;
  2879. }
  2880. static void __exit happy_meal_exit(void)
  2881. {
  2882. #ifdef CONFIG_SBUS
  2883. happy_meal_sbus_exit();
  2884. #endif
  2885. #ifdef CONFIG_PCI
  2886. happy_meal_pci_exit();
  2887. #endif
  2888. }
  2889. module_init(happy_meal_probe);
  2890. module_exit(happy_meal_exit);