sunhme.c 92 KB

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