sunhme.c 92 KB

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