sunhme.c 92 KB

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