sb1250-mac.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  1. /*
  2. * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. *
  19. * This driver is designed for the Broadcom SiByte SOC built-in
  20. * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/string.h>
  25. #include <linux/timer.h>
  26. #include <linux/errno.h>
  27. #include <linux/ioport.h>
  28. #include <linux/slab.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/bitops.h>
  35. #include <asm/processor.h> /* Processor type for cache alignment. */
  36. #include <asm/io.h>
  37. #include <asm/cache.h>
  38. /* This is only here until the firmware is ready. In that case,
  39. the firmware leaves the ethernet address in the register for us. */
  40. #ifdef CONFIG_SIBYTE_STANDALONE
  41. #define SBMAC_ETH0_HWADDR "40:00:00:00:01:00"
  42. #define SBMAC_ETH1_HWADDR "40:00:00:00:01:01"
  43. #define SBMAC_ETH2_HWADDR "40:00:00:00:01:02"
  44. #define SBMAC_ETH3_HWADDR "40:00:00:00:01:03"
  45. #endif
  46. /* These identify the driver base version and may not be removed. */
  47. #if 0
  48. static char version1[] __devinitdata =
  49. "sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg\n";
  50. #endif
  51. /* Operational parameters that usually are not changed. */
  52. #define CONFIG_SBMAC_COALESCE
  53. #define MAX_UNITS 4 /* More are supported, limit only on options */
  54. /* Time in jiffies before concluding the transmitter is hung. */
  55. #define TX_TIMEOUT (2*HZ)
  56. MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
  57. MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
  58. /* A few user-configurable values which may be modified when a driver
  59. module is loaded. */
  60. /* 1 normal messages, 0 quiet .. 7 verbose. */
  61. static int debug = 1;
  62. module_param(debug, int, S_IRUGO);
  63. MODULE_PARM_DESC(debug, "Debug messages");
  64. /* mii status msgs */
  65. static int noisy_mii = 1;
  66. module_param(noisy_mii, int, S_IRUGO);
  67. MODULE_PARM_DESC(noisy_mii, "MII status messages");
  68. /* Used to pass the media type, etc.
  69. Both 'options[]' and 'full_duplex[]' should exist for driver
  70. interoperability.
  71. The media type is usually passed in 'options[]'.
  72. */
  73. #ifdef MODULE
  74. static int options[MAX_UNITS] = {-1, -1, -1, -1};
  75. module_param_array(options, int, NULL, S_IRUGO);
  76. MODULE_PARM_DESC(options, "1-" __MODULE_STRING(MAX_UNITS));
  77. static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1};
  78. module_param_array(full_duplex, int, NULL, S_IRUGO);
  79. MODULE_PARM_DESC(full_duplex, "1-" __MODULE_STRING(MAX_UNITS));
  80. #endif
  81. #ifdef CONFIG_SBMAC_COALESCE
  82. static int int_pktcnt_tx = 255;
  83. module_param(int_pktcnt_tx, int, S_IRUGO);
  84. MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
  85. static int int_timeout_tx = 255;
  86. module_param(int_timeout_tx, int, S_IRUGO);
  87. MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
  88. static int int_pktcnt_rx = 64;
  89. module_param(int_pktcnt_rx, int, S_IRUGO);
  90. MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
  91. static int int_timeout_rx = 64;
  92. module_param(int_timeout_rx, int, S_IRUGO);
  93. MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
  94. #endif
  95. #include <asm/sibyte/sb1250.h>
  96. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  97. #include <asm/sibyte/bcm1480_regs.h>
  98. #include <asm/sibyte/bcm1480_int.h>
  99. #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
  100. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  101. #include <asm/sibyte/sb1250_regs.h>
  102. #include <asm/sibyte/sb1250_int.h>
  103. #else
  104. #error invalid SiByte MAC configuation
  105. #endif
  106. #include <asm/sibyte/sb1250_scd.h>
  107. #include <asm/sibyte/sb1250_mac.h>
  108. #include <asm/sibyte/sb1250_dma.h>
  109. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  110. #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
  111. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  112. #define UNIT_INT(n) (K_INT_MAC_0 + (n))
  113. #else
  114. #error invalid SiByte MAC configuation
  115. #endif
  116. /**********************************************************************
  117. * Simple types
  118. ********************************************************************* */
  119. typedef enum { sbmac_speed_auto, sbmac_speed_10,
  120. sbmac_speed_100, sbmac_speed_1000 } sbmac_speed_t;
  121. typedef enum { sbmac_duplex_auto, sbmac_duplex_half,
  122. sbmac_duplex_full } sbmac_duplex_t;
  123. typedef enum { sbmac_fc_auto, sbmac_fc_disabled, sbmac_fc_frame,
  124. sbmac_fc_collision, sbmac_fc_carrier } sbmac_fc_t;
  125. typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
  126. sbmac_state_broken } sbmac_state_t;
  127. /**********************************************************************
  128. * Macros
  129. ********************************************************************* */
  130. #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
  131. (d)->sbdma_dscrtable : (d)->f+1)
  132. #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
  133. #define SBMAC_MAX_TXDESCR 256
  134. #define SBMAC_MAX_RXDESCR 256
  135. #define ETHER_ALIGN 2
  136. #define ETHER_ADDR_LEN 6
  137. #define ENET_PACKET_SIZE 1518
  138. /*#define ENET_PACKET_SIZE 9216 */
  139. /**********************************************************************
  140. * DMA Descriptor structure
  141. ********************************************************************* */
  142. typedef struct sbdmadscr_s {
  143. uint64_t dscr_a;
  144. uint64_t dscr_b;
  145. } sbdmadscr_t;
  146. typedef unsigned long paddr_t;
  147. /**********************************************************************
  148. * DMA Controller structure
  149. ********************************************************************* */
  150. typedef struct sbmacdma_s {
  151. /*
  152. * This stuff is used to identify the channel and the registers
  153. * associated with it.
  154. */
  155. struct sbmac_softc *sbdma_eth; /* back pointer to associated MAC */
  156. int sbdma_channel; /* channel number */
  157. int sbdma_txdir; /* direction (1=transmit) */
  158. int sbdma_maxdescr; /* total # of descriptors in ring */
  159. #ifdef CONFIG_SBMAC_COALESCE
  160. int sbdma_int_pktcnt; /* # descriptors rx/tx before interrupt*/
  161. int sbdma_int_timeout; /* # usec rx/tx interrupt */
  162. #endif
  163. volatile void __iomem *sbdma_config0; /* DMA config register 0 */
  164. volatile void __iomem *sbdma_config1; /* DMA config register 1 */
  165. volatile void __iomem *sbdma_dscrbase; /* Descriptor base address */
  166. volatile void __iomem *sbdma_dscrcnt; /* Descriptor count register */
  167. volatile void __iomem *sbdma_curdscr; /* current descriptor address */
  168. volatile void __iomem *sbdma_oodpktlost;/* pkt drop (rx only) */
  169. /*
  170. * This stuff is for maintenance of the ring
  171. */
  172. sbdmadscr_t *sbdma_dscrtable_unaligned;
  173. sbdmadscr_t *sbdma_dscrtable; /* base of descriptor table */
  174. sbdmadscr_t *sbdma_dscrtable_end; /* end of descriptor table */
  175. struct sk_buff **sbdma_ctxtable; /* context table, one per descr */
  176. paddr_t sbdma_dscrtable_phys; /* and also the phys addr */
  177. sbdmadscr_t *sbdma_addptr; /* next dscr for sw to add */
  178. sbdmadscr_t *sbdma_remptr; /* next dscr for sw to remove */
  179. } sbmacdma_t;
  180. /**********************************************************************
  181. * Ethernet softc structure
  182. ********************************************************************* */
  183. struct sbmac_softc {
  184. /*
  185. * Linux-specific things
  186. */
  187. struct net_device *sbm_dev; /* pointer to linux device */
  188. spinlock_t sbm_lock; /* spin lock */
  189. struct timer_list sbm_timer; /* for monitoring MII */
  190. struct net_device_stats sbm_stats;
  191. int sbm_devflags; /* current device flags */
  192. int sbm_phy_oldbmsr;
  193. int sbm_phy_oldanlpar;
  194. int sbm_phy_oldk1stsr;
  195. int sbm_phy_oldlinkstat;
  196. int sbm_buffersize;
  197. unsigned char sbm_phys[2];
  198. /*
  199. * Controller-specific things
  200. */
  201. void __iomem *sbm_base; /* MAC's base address */
  202. sbmac_state_t sbm_state; /* current state */
  203. volatile void __iomem *sbm_macenable; /* MAC Enable Register */
  204. volatile void __iomem *sbm_maccfg; /* MAC Configuration Register */
  205. volatile void __iomem *sbm_fifocfg; /* FIFO configuration register */
  206. volatile void __iomem *sbm_framecfg; /* Frame configuration register */
  207. volatile void __iomem *sbm_rxfilter; /* receive filter register */
  208. volatile void __iomem *sbm_isr; /* Interrupt status register */
  209. volatile void __iomem *sbm_imr; /* Interrupt mask register */
  210. volatile void __iomem *sbm_mdio; /* MDIO register */
  211. sbmac_speed_t sbm_speed; /* current speed */
  212. sbmac_duplex_t sbm_duplex; /* current duplex */
  213. sbmac_fc_t sbm_fc; /* current flow control setting */
  214. unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
  215. sbmacdma_t sbm_txdma; /* for now, only use channel 0 */
  216. sbmacdma_t sbm_rxdma;
  217. int rx_hw_checksum;
  218. int sbe_idx;
  219. };
  220. /**********************************************************************
  221. * Externs
  222. ********************************************************************* */
  223. /**********************************************************************
  224. * Prototypes
  225. ********************************************************************* */
  226. static void sbdma_initctx(sbmacdma_t *d,
  227. struct sbmac_softc *s,
  228. int chan,
  229. int txrx,
  230. int maxdescr);
  231. static void sbdma_channel_start(sbmacdma_t *d, int rxtx);
  232. static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *m);
  233. static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *m);
  234. static void sbdma_emptyring(sbmacdma_t *d);
  235. static void sbdma_fillring(sbmacdma_t *d);
  236. static int sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d, int work_to_do, int poll);
  237. static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d, int poll);
  238. static int sbmac_initctx(struct sbmac_softc *s);
  239. static void sbmac_channel_start(struct sbmac_softc *s);
  240. static void sbmac_channel_stop(struct sbmac_softc *s);
  241. static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *,sbmac_state_t);
  242. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff);
  243. static uint64_t sbmac_addr2reg(unsigned char *ptr);
  244. static irqreturn_t sbmac_intr(int irq,void *dev_instance);
  245. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
  246. static void sbmac_setmulti(struct sbmac_softc *sc);
  247. static int sbmac_init(struct net_device *dev, int idx);
  248. static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed);
  249. static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc);
  250. static int sbmac_open(struct net_device *dev);
  251. static void sbmac_timer(unsigned long data);
  252. static void sbmac_tx_timeout (struct net_device *dev);
  253. static struct net_device_stats *sbmac_get_stats(struct net_device *dev);
  254. static void sbmac_set_rx_mode(struct net_device *dev);
  255. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  256. static int sbmac_close(struct net_device *dev);
  257. static int sbmac_poll(struct net_device *poll_dev, int *budget);
  258. static int sbmac_mii_poll(struct sbmac_softc *s,int noisy);
  259. static int sbmac_mii_probe(struct net_device *dev);
  260. static void sbmac_mii_sync(struct sbmac_softc *s);
  261. static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt);
  262. static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx);
  263. static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
  264. unsigned int regval);
  265. /**********************************************************************
  266. * Globals
  267. ********************************************************************* */
  268. static uint64_t sbmac_orig_hwaddr[MAX_UNITS];
  269. /**********************************************************************
  270. * MDIO constants
  271. ********************************************************************* */
  272. #define MII_COMMAND_START 0x01
  273. #define MII_COMMAND_READ 0x02
  274. #define MII_COMMAND_WRITE 0x01
  275. #define MII_COMMAND_ACK 0x02
  276. #define BMCR_RESET 0x8000
  277. #define BMCR_LOOPBACK 0x4000
  278. #define BMCR_SPEED0 0x2000
  279. #define BMCR_ANENABLE 0x1000
  280. #define BMCR_POWERDOWN 0x0800
  281. #define BMCR_ISOLATE 0x0400
  282. #define BMCR_RESTARTAN 0x0200
  283. #define BMCR_DUPLEX 0x0100
  284. #define BMCR_COLTEST 0x0080
  285. #define BMCR_SPEED1 0x0040
  286. #define BMCR_SPEED1000 BMCR_SPEED1
  287. #define BMCR_SPEED100 BMCR_SPEED0
  288. #define BMCR_SPEED10 0
  289. #define BMSR_100BT4 0x8000
  290. #define BMSR_100BT_FDX 0x4000
  291. #define BMSR_100BT_HDX 0x2000
  292. #define BMSR_10BT_FDX 0x1000
  293. #define BMSR_10BT_HDX 0x0800
  294. #define BMSR_100BT2_FDX 0x0400
  295. #define BMSR_100BT2_HDX 0x0200
  296. #define BMSR_1000BT_XSR 0x0100
  297. #define BMSR_PRESUP 0x0040
  298. #define BMSR_ANCOMPLT 0x0020
  299. #define BMSR_REMFAULT 0x0010
  300. #define BMSR_AUTONEG 0x0008
  301. #define BMSR_LINKSTAT 0x0004
  302. #define BMSR_JABDETECT 0x0002
  303. #define BMSR_EXTCAPAB 0x0001
  304. #define PHYIDR1 0x2000
  305. #define PHYIDR2 0x5C60
  306. #define ANAR_NP 0x8000
  307. #define ANAR_RF 0x2000
  308. #define ANAR_ASYPAUSE 0x0800
  309. #define ANAR_PAUSE 0x0400
  310. #define ANAR_T4 0x0200
  311. #define ANAR_TXFD 0x0100
  312. #define ANAR_TXHD 0x0080
  313. #define ANAR_10FD 0x0040
  314. #define ANAR_10HD 0x0020
  315. #define ANAR_PSB 0x0001
  316. #define ANLPAR_NP 0x8000
  317. #define ANLPAR_ACK 0x4000
  318. #define ANLPAR_RF 0x2000
  319. #define ANLPAR_ASYPAUSE 0x0800
  320. #define ANLPAR_PAUSE 0x0400
  321. #define ANLPAR_T4 0x0200
  322. #define ANLPAR_TXFD 0x0100
  323. #define ANLPAR_TXHD 0x0080
  324. #define ANLPAR_10FD 0x0040
  325. #define ANLPAR_10HD 0x0020
  326. #define ANLPAR_PSB 0x0001 /* 802.3 */
  327. #define ANER_PDF 0x0010
  328. #define ANER_LPNPABLE 0x0008
  329. #define ANER_NPABLE 0x0004
  330. #define ANER_PAGERX 0x0002
  331. #define ANER_LPANABLE 0x0001
  332. #define ANNPTR_NP 0x8000
  333. #define ANNPTR_MP 0x2000
  334. #define ANNPTR_ACK2 0x1000
  335. #define ANNPTR_TOGTX 0x0800
  336. #define ANNPTR_CODE 0x0008
  337. #define ANNPRR_NP 0x8000
  338. #define ANNPRR_MP 0x2000
  339. #define ANNPRR_ACK3 0x1000
  340. #define ANNPRR_TOGTX 0x0800
  341. #define ANNPRR_CODE 0x0008
  342. #define K1TCR_TESTMODE 0x0000
  343. #define K1TCR_MSMCE 0x1000
  344. #define K1TCR_MSCV 0x0800
  345. #define K1TCR_RPTR 0x0400
  346. #define K1TCR_1000BT_FDX 0x200
  347. #define K1TCR_1000BT_HDX 0x100
  348. #define K1STSR_MSMCFLT 0x8000
  349. #define K1STSR_MSCFGRES 0x4000
  350. #define K1STSR_LRSTAT 0x2000
  351. #define K1STSR_RRSTAT 0x1000
  352. #define K1STSR_LP1KFD 0x0800
  353. #define K1STSR_LP1KHD 0x0400
  354. #define K1STSR_LPASMDIR 0x0200
  355. #define K1SCR_1KX_FDX 0x8000
  356. #define K1SCR_1KX_HDX 0x4000
  357. #define K1SCR_1KT_FDX 0x2000
  358. #define K1SCR_1KT_HDX 0x1000
  359. #define STRAP_PHY1 0x0800
  360. #define STRAP_NCMODE 0x0400
  361. #define STRAP_MANMSCFG 0x0200
  362. #define STRAP_ANENABLE 0x0100
  363. #define STRAP_MSVAL 0x0080
  364. #define STRAP_1KHDXADV 0x0010
  365. #define STRAP_1KFDXADV 0x0008
  366. #define STRAP_100ADV 0x0004
  367. #define STRAP_SPEEDSEL 0x0000
  368. #define STRAP_SPEED100 0x0001
  369. #define PHYSUP_SPEED1000 0x10
  370. #define PHYSUP_SPEED100 0x08
  371. #define PHYSUP_SPEED10 0x00
  372. #define PHYSUP_LINKUP 0x04
  373. #define PHYSUP_FDX 0x02
  374. #define MII_BMCR 0x00 /* Basic mode control register (rw) */
  375. #define MII_BMSR 0x01 /* Basic mode status register (ro) */
  376. #define MII_PHYIDR1 0x02
  377. #define MII_PHYIDR2 0x03
  378. #define MII_K1STSR 0x0A /* 1K Status Register (ro) */
  379. #define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
  380. #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
  381. #define ENABLE 1
  382. #define DISABLE 0
  383. /**********************************************************************
  384. * SBMAC_MII_SYNC(s)
  385. *
  386. * Synchronize with the MII - send a pattern of bits to the MII
  387. * that will guarantee that it is ready to accept a command.
  388. *
  389. * Input parameters:
  390. * s - sbmac structure
  391. *
  392. * Return value:
  393. * nothing
  394. ********************************************************************* */
  395. static void sbmac_mii_sync(struct sbmac_softc *s)
  396. {
  397. int cnt;
  398. uint64_t bits;
  399. int mac_mdio_genc;
  400. mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
  401. bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
  402. __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
  403. for (cnt = 0; cnt < 32; cnt++) {
  404. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
  405. __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
  406. }
  407. }
  408. /**********************************************************************
  409. * SBMAC_MII_SENDDATA(s,data,bitcnt)
  410. *
  411. * Send some bits to the MII. The bits to be sent are right-
  412. * justified in the 'data' parameter.
  413. *
  414. * Input parameters:
  415. * s - sbmac structure
  416. * data - data to send
  417. * bitcnt - number of bits to send
  418. ********************************************************************* */
  419. static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt)
  420. {
  421. int i;
  422. uint64_t bits;
  423. unsigned int curmask;
  424. int mac_mdio_genc;
  425. mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
  426. bits = M_MAC_MDIO_DIR_OUTPUT;
  427. __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
  428. curmask = 1 << (bitcnt - 1);
  429. for (i = 0; i < bitcnt; i++) {
  430. if (data & curmask)
  431. bits |= M_MAC_MDIO_OUT;
  432. else bits &= ~M_MAC_MDIO_OUT;
  433. __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
  434. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
  435. __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
  436. curmask >>= 1;
  437. }
  438. }
  439. /**********************************************************************
  440. * SBMAC_MII_READ(s,phyaddr,regidx)
  441. *
  442. * Read a PHY register.
  443. *
  444. * Input parameters:
  445. * s - sbmac structure
  446. * phyaddr - PHY's address
  447. * regidx = index of register to read
  448. *
  449. * Return value:
  450. * value read, or 0 if an error occurred.
  451. ********************************************************************* */
  452. static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
  453. {
  454. int idx;
  455. int error;
  456. int regval;
  457. int mac_mdio_genc;
  458. /*
  459. * Synchronize ourselves so that the PHY knows the next
  460. * thing coming down is a command
  461. */
  462. sbmac_mii_sync(s);
  463. /*
  464. * Send the data to the PHY. The sequence is
  465. * a "start" command (2 bits)
  466. * a "read" command (2 bits)
  467. * the PHY addr (5 bits)
  468. * the register index (5 bits)
  469. */
  470. sbmac_mii_senddata(s,MII_COMMAND_START, 2);
  471. sbmac_mii_senddata(s,MII_COMMAND_READ, 2);
  472. sbmac_mii_senddata(s,phyaddr, 5);
  473. sbmac_mii_senddata(s,regidx, 5);
  474. mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
  475. /*
  476. * Switch the port around without a clock transition.
  477. */
  478. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
  479. /*
  480. * Send out a clock pulse to signal we want the status
  481. */
  482. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
  483. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
  484. /*
  485. * If an error occurred, the PHY will signal '1' back
  486. */
  487. error = __raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN;
  488. /*
  489. * Issue an 'idle' clock pulse, but keep the direction
  490. * the same.
  491. */
  492. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
  493. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
  494. regval = 0;
  495. for (idx = 0; idx < 16; idx++) {
  496. regval <<= 1;
  497. if (error == 0) {
  498. if (__raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN)
  499. regval |= 1;
  500. }
  501. __raw_writeq(M_MAC_MDIO_DIR_INPUT|M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
  502. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
  503. }
  504. /* Switch back to output */
  505. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
  506. if (error == 0)
  507. return regval;
  508. return 0;
  509. }
  510. /**********************************************************************
  511. * SBMAC_MII_WRITE(s,phyaddr,regidx,regval)
  512. *
  513. * Write a value to a PHY register.
  514. *
  515. * Input parameters:
  516. * s - sbmac structure
  517. * phyaddr - PHY to use
  518. * regidx - register within the PHY
  519. * regval - data to write to register
  520. *
  521. * Return value:
  522. * nothing
  523. ********************************************************************* */
  524. static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
  525. unsigned int regval)
  526. {
  527. int mac_mdio_genc;
  528. sbmac_mii_sync(s);
  529. sbmac_mii_senddata(s,MII_COMMAND_START,2);
  530. sbmac_mii_senddata(s,MII_COMMAND_WRITE,2);
  531. sbmac_mii_senddata(s,phyaddr, 5);
  532. sbmac_mii_senddata(s,regidx, 5);
  533. sbmac_mii_senddata(s,MII_COMMAND_ACK,2);
  534. sbmac_mii_senddata(s,regval,16);
  535. mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
  536. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
  537. }
  538. /**********************************************************************
  539. * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
  540. *
  541. * Initialize a DMA channel context. Since there are potentially
  542. * eight DMA channels per MAC, it's nice to do this in a standard
  543. * way.
  544. *
  545. * Input parameters:
  546. * d - sbmacdma_t structure (DMA channel context)
  547. * s - sbmac_softc structure (pointer to a MAC)
  548. * chan - channel number (0..1 right now)
  549. * txrx - Identifies DMA_TX or DMA_RX for channel direction
  550. * maxdescr - number of descriptors
  551. *
  552. * Return value:
  553. * nothing
  554. ********************************************************************* */
  555. static void sbdma_initctx(sbmacdma_t *d,
  556. struct sbmac_softc *s,
  557. int chan,
  558. int txrx,
  559. int maxdescr)
  560. {
  561. #ifdef CONFIG_SBMAC_COALESCE
  562. int int_pktcnt, int_timeout;
  563. #endif
  564. /*
  565. * Save away interesting stuff in the structure
  566. */
  567. d->sbdma_eth = s;
  568. d->sbdma_channel = chan;
  569. d->sbdma_txdir = txrx;
  570. #if 0
  571. /* RMON clearing */
  572. s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
  573. #endif
  574. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BYTES)));
  575. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_COLLISIONS)));
  576. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_LATE_COL)));
  577. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_EX_COL)));
  578. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_FCS_ERROR)));
  579. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_ABORT)));
  580. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BAD)));
  581. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_GOOD)));
  582. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_RUNT)));
  583. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_OVERSIZE)));
  584. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BYTES)));
  585. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_MCAST)));
  586. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BCAST)));
  587. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BAD)));
  588. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_GOOD)));
  589. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_RUNT)));
  590. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_OVERSIZE)));
  591. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_FCS_ERROR)));
  592. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_LENGTH_ERROR)));
  593. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_CODE_ERROR)));
  594. __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_ALIGN_ERROR)));
  595. /*
  596. * initialize register pointers
  597. */
  598. d->sbdma_config0 =
  599. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
  600. d->sbdma_config1 =
  601. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
  602. d->sbdma_dscrbase =
  603. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
  604. d->sbdma_dscrcnt =
  605. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
  606. d->sbdma_curdscr =
  607. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
  608. if (d->sbdma_txdir)
  609. d->sbdma_oodpktlost = NULL;
  610. else
  611. d->sbdma_oodpktlost =
  612. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
  613. /*
  614. * Allocate memory for the ring
  615. */
  616. d->sbdma_maxdescr = maxdescr;
  617. d->sbdma_dscrtable_unaligned =
  618. d->sbdma_dscrtable = (sbdmadscr_t *)
  619. kmalloc((d->sbdma_maxdescr+1)*sizeof(sbdmadscr_t), GFP_KERNEL);
  620. /*
  621. * The descriptor table must be aligned to at least 16 bytes or the
  622. * MAC will corrupt it.
  623. */
  624. d->sbdma_dscrtable = (sbdmadscr_t *)
  625. ALIGN((unsigned long)d->sbdma_dscrtable, sizeof(sbdmadscr_t));
  626. memset(d->sbdma_dscrtable,0,d->sbdma_maxdescr*sizeof(sbdmadscr_t));
  627. d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
  628. d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
  629. /*
  630. * And context table
  631. */
  632. d->sbdma_ctxtable = (struct sk_buff **)
  633. kmalloc(d->sbdma_maxdescr*sizeof(struct sk_buff *), GFP_KERNEL);
  634. memset(d->sbdma_ctxtable,0,d->sbdma_maxdescr*sizeof(struct sk_buff *));
  635. #ifdef CONFIG_SBMAC_COALESCE
  636. /*
  637. * Setup Rx/Tx DMA coalescing defaults
  638. */
  639. int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
  640. if ( int_pktcnt ) {
  641. d->sbdma_int_pktcnt = int_pktcnt;
  642. } else {
  643. d->sbdma_int_pktcnt = 1;
  644. }
  645. int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
  646. if ( int_timeout ) {
  647. d->sbdma_int_timeout = int_timeout;
  648. } else {
  649. d->sbdma_int_timeout = 0;
  650. }
  651. #endif
  652. }
  653. /**********************************************************************
  654. * SBDMA_CHANNEL_START(d)
  655. *
  656. * Initialize the hardware registers for a DMA channel.
  657. *
  658. * Input parameters:
  659. * d - DMA channel to init (context must be previously init'd
  660. * rxtx - DMA_RX or DMA_TX depending on what type of channel
  661. *
  662. * Return value:
  663. * nothing
  664. ********************************************************************* */
  665. static void sbdma_channel_start(sbmacdma_t *d, int rxtx )
  666. {
  667. /*
  668. * Turn on the DMA channel
  669. */
  670. #ifdef CONFIG_SBMAC_COALESCE
  671. __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
  672. 0, d->sbdma_config1);
  673. __raw_writeq(M_DMA_EOP_INT_EN |
  674. V_DMA_RINGSZ(d->sbdma_maxdescr) |
  675. V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
  676. 0, d->sbdma_config0);
  677. #else
  678. __raw_writeq(0, d->sbdma_config1);
  679. __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
  680. 0, d->sbdma_config0);
  681. #endif
  682. __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
  683. /*
  684. * Initialize ring pointers
  685. */
  686. d->sbdma_addptr = d->sbdma_dscrtable;
  687. d->sbdma_remptr = d->sbdma_dscrtable;
  688. }
  689. /**********************************************************************
  690. * SBDMA_CHANNEL_STOP(d)
  691. *
  692. * Initialize the hardware registers for a DMA channel.
  693. *
  694. * Input parameters:
  695. * d - DMA channel to init (context must be previously init'd
  696. *
  697. * Return value:
  698. * nothing
  699. ********************************************************************* */
  700. static void sbdma_channel_stop(sbmacdma_t *d)
  701. {
  702. /*
  703. * Turn off the DMA channel
  704. */
  705. __raw_writeq(0, d->sbdma_config1);
  706. __raw_writeq(0, d->sbdma_dscrbase);
  707. __raw_writeq(0, d->sbdma_config0);
  708. /*
  709. * Zero ring pointers
  710. */
  711. d->sbdma_addptr = NULL;
  712. d->sbdma_remptr = NULL;
  713. }
  714. static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
  715. {
  716. unsigned long addr;
  717. unsigned long newaddr;
  718. addr = (unsigned long) skb->data;
  719. newaddr = (addr + power2 - 1) & ~(power2 - 1);
  720. skb_reserve(skb,newaddr-addr+offset);
  721. }
  722. /**********************************************************************
  723. * SBDMA_ADD_RCVBUFFER(d,sb)
  724. *
  725. * Add a buffer to the specified DMA channel. For receive channels,
  726. * this queues a buffer for inbound packets.
  727. *
  728. * Input parameters:
  729. * d - DMA channel descriptor
  730. * sb - sk_buff to add, or NULL if we should allocate one
  731. *
  732. * Return value:
  733. * 0 if buffer could not be added (ring is full)
  734. * 1 if buffer added successfully
  735. ********************************************************************* */
  736. static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
  737. {
  738. sbdmadscr_t *dsc;
  739. sbdmadscr_t *nextdsc;
  740. struct sk_buff *sb_new = NULL;
  741. int pktsize = ENET_PACKET_SIZE;
  742. /* get pointer to our current place in the ring */
  743. dsc = d->sbdma_addptr;
  744. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  745. /*
  746. * figure out if the ring is full - if the next descriptor
  747. * is the same as the one that we're going to remove from
  748. * the ring, the ring is full
  749. */
  750. if (nextdsc == d->sbdma_remptr) {
  751. return -ENOSPC;
  752. }
  753. /*
  754. * Allocate a sk_buff if we don't already have one.
  755. * If we do have an sk_buff, reset it so that it's empty.
  756. *
  757. * Note: sk_buffs don't seem to be guaranteed to have any sort
  758. * of alignment when they are allocated. Therefore, allocate enough
  759. * extra space to make sure that:
  760. *
  761. * 1. the data does not start in the middle of a cache line.
  762. * 2. The data does not end in the middle of a cache line
  763. * 3. The buffer can be aligned such that the IP addresses are
  764. * naturally aligned.
  765. *
  766. * Remember, the SOCs MAC writes whole cache lines at a time,
  767. * without reading the old contents first. So, if the sk_buff's
  768. * data portion starts in the middle of a cache line, the SOC
  769. * DMA will trash the beginning (and ending) portions.
  770. */
  771. if (sb == NULL) {
  772. sb_new = dev_alloc_skb(ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN);
  773. if (sb_new == NULL) {
  774. printk(KERN_INFO "%s: sk_buff allocation failed\n",
  775. d->sbdma_eth->sbm_dev->name);
  776. return -ENOBUFS;
  777. }
  778. sbdma_align_skb(sb_new, SMP_CACHE_BYTES, ETHER_ALIGN);
  779. }
  780. else {
  781. sb_new = sb;
  782. /*
  783. * nothing special to reinit buffer, it's already aligned
  784. * and sb->data already points to a good place.
  785. */
  786. }
  787. /*
  788. * fill in the descriptor
  789. */
  790. #ifdef CONFIG_SBMAC_COALESCE
  791. /*
  792. * Do not interrupt per DMA transfer.
  793. */
  794. dsc->dscr_a = virt_to_phys(sb_new->data) |
  795. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) | 0;
  796. #else
  797. dsc->dscr_a = virt_to_phys(sb_new->data) |
  798. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
  799. M_DMA_DSCRA_INTERRUPT;
  800. #endif
  801. /* receiving: no options */
  802. dsc->dscr_b = 0;
  803. /*
  804. * fill in the context
  805. */
  806. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
  807. /*
  808. * point at next packet
  809. */
  810. d->sbdma_addptr = nextdsc;
  811. /*
  812. * Give the buffer to the DMA engine.
  813. */
  814. __raw_writeq(1, d->sbdma_dscrcnt);
  815. return 0; /* we did it */
  816. }
  817. /**********************************************************************
  818. * SBDMA_ADD_TXBUFFER(d,sb)
  819. *
  820. * Add a transmit buffer to the specified DMA channel, causing a
  821. * transmit to start.
  822. *
  823. * Input parameters:
  824. * d - DMA channel descriptor
  825. * sb - sk_buff to add
  826. *
  827. * Return value:
  828. * 0 transmit queued successfully
  829. * otherwise error code
  830. ********************************************************************* */
  831. static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *sb)
  832. {
  833. sbdmadscr_t *dsc;
  834. sbdmadscr_t *nextdsc;
  835. uint64_t phys;
  836. uint64_t ncb;
  837. int length;
  838. /* get pointer to our current place in the ring */
  839. dsc = d->sbdma_addptr;
  840. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  841. /*
  842. * figure out if the ring is full - if the next descriptor
  843. * is the same as the one that we're going to remove from
  844. * the ring, the ring is full
  845. */
  846. if (nextdsc == d->sbdma_remptr) {
  847. return -ENOSPC;
  848. }
  849. /*
  850. * Under Linux, it's not necessary to copy/coalesce buffers
  851. * like it is on NetBSD. We think they're all contiguous,
  852. * but that may not be true for GBE.
  853. */
  854. length = sb->len;
  855. /*
  856. * fill in the descriptor. Note that the number of cache
  857. * blocks in the descriptor is the number of blocks
  858. * *spanned*, so we need to add in the offset (if any)
  859. * while doing the calculation.
  860. */
  861. phys = virt_to_phys(sb->data);
  862. ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
  863. dsc->dscr_a = phys |
  864. V_DMA_DSCRA_A_SIZE(ncb) |
  865. #ifndef CONFIG_SBMAC_COALESCE
  866. M_DMA_DSCRA_INTERRUPT |
  867. #endif
  868. M_DMA_ETHTX_SOP;
  869. /* transmitting: set outbound options and length */
  870. dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
  871. V_DMA_DSCRB_PKT_SIZE(length);
  872. /*
  873. * fill in the context
  874. */
  875. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
  876. /*
  877. * point at next packet
  878. */
  879. d->sbdma_addptr = nextdsc;
  880. /*
  881. * Give the buffer to the DMA engine.
  882. */
  883. __raw_writeq(1, d->sbdma_dscrcnt);
  884. return 0; /* we did it */
  885. }
  886. /**********************************************************************
  887. * SBDMA_EMPTYRING(d)
  888. *
  889. * Free all allocated sk_buffs on the specified DMA channel;
  890. *
  891. * Input parameters:
  892. * d - DMA channel
  893. *
  894. * Return value:
  895. * nothing
  896. ********************************************************************* */
  897. static void sbdma_emptyring(sbmacdma_t *d)
  898. {
  899. int idx;
  900. struct sk_buff *sb;
  901. for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
  902. sb = d->sbdma_ctxtable[idx];
  903. if (sb) {
  904. dev_kfree_skb(sb);
  905. d->sbdma_ctxtable[idx] = NULL;
  906. }
  907. }
  908. }
  909. /**********************************************************************
  910. * SBDMA_FILLRING(d)
  911. *
  912. * Fill the specified DMA channel (must be receive channel)
  913. * with sk_buffs
  914. *
  915. * Input parameters:
  916. * d - DMA channel
  917. *
  918. * Return value:
  919. * nothing
  920. ********************************************************************* */
  921. static void sbdma_fillring(sbmacdma_t *d)
  922. {
  923. int idx;
  924. for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
  925. if (sbdma_add_rcvbuffer(d,NULL) != 0)
  926. break;
  927. }
  928. }
  929. #ifdef CONFIG_NET_POLL_CONTROLLER
  930. static void sbmac_netpoll(struct net_device *netdev)
  931. {
  932. struct sbmac_softc *sc = netdev_priv(netdev);
  933. int irq = sc->sbm_dev->irq;
  934. __raw_writeq(0, sc->sbm_imr);
  935. sbmac_intr(irq, netdev, NULL);
  936. #ifdef CONFIG_SBMAC_COALESCE
  937. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  938. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  939. sc->sbm_imr);
  940. #else
  941. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  942. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  943. #endif
  944. }
  945. #endif
  946. /**********************************************************************
  947. * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
  948. *
  949. * Process "completed" receive buffers on the specified DMA channel.
  950. *
  951. * Input parameters:
  952. * sc - softc structure
  953. * d - DMA channel context
  954. * work_to_do - no. of packets to process before enabling interrupt
  955. * again (for NAPI)
  956. * poll - 1: using polling (for NAPI)
  957. *
  958. * Return value:
  959. * nothing
  960. ********************************************************************* */
  961. static int sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d,
  962. int work_to_do, int poll)
  963. {
  964. int curidx;
  965. int hwidx;
  966. sbdmadscr_t *dsc;
  967. struct sk_buff *sb;
  968. int len;
  969. int work_done = 0;
  970. int dropped = 0;
  971. prefetch(d);
  972. again:
  973. /* Check if the HW dropped any frames */
  974. sc->sbm_stats.rx_fifo_errors
  975. += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
  976. __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
  977. while (work_to_do-- > 0) {
  978. /*
  979. * figure out where we are (as an index) and where
  980. * the hardware is (also as an index)
  981. *
  982. * This could be done faster if (for example) the
  983. * descriptor table was page-aligned and contiguous in
  984. * both virtual and physical memory -- you could then
  985. * just compare the low-order bits of the virtual address
  986. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  987. */
  988. dsc = d->sbdma_remptr;
  989. curidx = dsc - d->sbdma_dscrtable;
  990. prefetch(dsc);
  991. prefetch(&d->sbdma_ctxtable[curidx]);
  992. hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  993. d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
  994. /*
  995. * If they're the same, that means we've processed all
  996. * of the descriptors up to (but not including) the one that
  997. * the hardware is working on right now.
  998. */
  999. if (curidx == hwidx)
  1000. goto done;
  1001. /*
  1002. * Otherwise, get the packet's sk_buff ptr back
  1003. */
  1004. sb = d->sbdma_ctxtable[curidx];
  1005. d->sbdma_ctxtable[curidx] = NULL;
  1006. len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
  1007. /*
  1008. * Check packet status. If good, process it.
  1009. * If not, silently drop it and put it back on the
  1010. * receive ring.
  1011. */
  1012. if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
  1013. /*
  1014. * Add a new buffer to replace the old one. If we fail
  1015. * to allocate a buffer, we're going to drop this
  1016. * packet and put it right back on the receive ring.
  1017. */
  1018. if (unlikely (sbdma_add_rcvbuffer(d,NULL) ==
  1019. -ENOBUFS)) {
  1020. sc->sbm_stats.rx_dropped++;
  1021. sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
  1022. /* No point in continuing at the moment */
  1023. printk(KERN_ERR "dropped packet (1)\n");
  1024. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1025. goto done;
  1026. } else {
  1027. /*
  1028. * Set length into the packet
  1029. */
  1030. skb_put(sb,len);
  1031. /*
  1032. * Buffer has been replaced on the
  1033. * receive ring. Pass the buffer to
  1034. * the kernel
  1035. */
  1036. sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
  1037. /* Check hw IPv4/TCP checksum if supported */
  1038. if (sc->rx_hw_checksum == ENABLE) {
  1039. if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
  1040. !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
  1041. sb->ip_summed = CHECKSUM_UNNECESSARY;
  1042. /* don't need to set sb->csum */
  1043. } else {
  1044. sb->ip_summed = CHECKSUM_NONE;
  1045. }
  1046. }
  1047. prefetch(sb->data);
  1048. prefetch((const void *)(((char *)sb->data)+32));
  1049. if (poll)
  1050. dropped = netif_receive_skb(sb);
  1051. else
  1052. dropped = netif_rx(sb);
  1053. if (dropped == NET_RX_DROP) {
  1054. sc->sbm_stats.rx_dropped++;
  1055. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1056. goto done;
  1057. }
  1058. else {
  1059. sc->sbm_stats.rx_bytes += len;
  1060. sc->sbm_stats.rx_packets++;
  1061. }
  1062. }
  1063. } else {
  1064. /*
  1065. * Packet was mangled somehow. Just drop it and
  1066. * put it back on the receive ring.
  1067. */
  1068. sc->sbm_stats.rx_errors++;
  1069. sbdma_add_rcvbuffer(d,sb);
  1070. }
  1071. /*
  1072. * .. and advance to the next buffer.
  1073. */
  1074. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1075. work_done++;
  1076. }
  1077. if (!poll) {
  1078. work_to_do = 32;
  1079. goto again; /* collect fifo drop statistics again */
  1080. }
  1081. done:
  1082. return work_done;
  1083. }
  1084. /**********************************************************************
  1085. * SBDMA_TX_PROCESS(sc,d)
  1086. *
  1087. * Process "completed" transmit buffers on the specified DMA channel.
  1088. * This is normally called within the interrupt service routine.
  1089. * Note that this isn't really ideal for priority channels, since
  1090. * it processes all of the packets on a given channel before
  1091. * returning.
  1092. *
  1093. * Input parameters:
  1094. * sc - softc structure
  1095. * d - DMA channel context
  1096. * poll - 1: using polling (for NAPI)
  1097. *
  1098. * Return value:
  1099. * nothing
  1100. ********************************************************************* */
  1101. static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d, int poll)
  1102. {
  1103. int curidx;
  1104. int hwidx;
  1105. sbdmadscr_t *dsc;
  1106. struct sk_buff *sb;
  1107. unsigned long flags;
  1108. int packets_handled = 0;
  1109. spin_lock_irqsave(&(sc->sbm_lock), flags);
  1110. if (d->sbdma_remptr == d->sbdma_addptr)
  1111. goto end_unlock;
  1112. hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  1113. d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
  1114. for (;;) {
  1115. /*
  1116. * figure out where we are (as an index) and where
  1117. * the hardware is (also as an index)
  1118. *
  1119. * This could be done faster if (for example) the
  1120. * descriptor table was page-aligned and contiguous in
  1121. * both virtual and physical memory -- you could then
  1122. * just compare the low-order bits of the virtual address
  1123. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  1124. */
  1125. curidx = d->sbdma_remptr - d->sbdma_dscrtable;
  1126. /*
  1127. * If they're the same, that means we've processed all
  1128. * of the descriptors up to (but not including) the one that
  1129. * the hardware is working on right now.
  1130. */
  1131. if (curidx == hwidx)
  1132. break;
  1133. /*
  1134. * Otherwise, get the packet's sk_buff ptr back
  1135. */
  1136. dsc = &(d->sbdma_dscrtable[curidx]);
  1137. sb = d->sbdma_ctxtable[curidx];
  1138. d->sbdma_ctxtable[curidx] = NULL;
  1139. /*
  1140. * Stats
  1141. */
  1142. sc->sbm_stats.tx_bytes += sb->len;
  1143. sc->sbm_stats.tx_packets++;
  1144. /*
  1145. * for transmits, we just free buffers.
  1146. */
  1147. dev_kfree_skb_irq(sb);
  1148. /*
  1149. * .. and advance to the next buffer.
  1150. */
  1151. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1152. packets_handled++;
  1153. }
  1154. /*
  1155. * Decide if we should wake up the protocol or not.
  1156. * Other drivers seem to do this when we reach a low
  1157. * watermark on the transmit queue.
  1158. */
  1159. if (packets_handled)
  1160. netif_wake_queue(d->sbdma_eth->sbm_dev);
  1161. end_unlock:
  1162. spin_unlock_irqrestore(&(sc->sbm_lock), flags);
  1163. }
  1164. /**********************************************************************
  1165. * SBMAC_INITCTX(s)
  1166. *
  1167. * Initialize an Ethernet context structure - this is called
  1168. * once per MAC on the 1250. Memory is allocated here, so don't
  1169. * call it again from inside the ioctl routines that bring the
  1170. * interface up/down
  1171. *
  1172. * Input parameters:
  1173. * s - sbmac context structure
  1174. *
  1175. * Return value:
  1176. * 0
  1177. ********************************************************************* */
  1178. static int sbmac_initctx(struct sbmac_softc *s)
  1179. {
  1180. /*
  1181. * figure out the addresses of some ports
  1182. */
  1183. s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
  1184. s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
  1185. s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
  1186. s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
  1187. s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
  1188. s->sbm_isr = s->sbm_base + R_MAC_STATUS;
  1189. s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
  1190. s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
  1191. s->sbm_phys[0] = 1;
  1192. s->sbm_phys[1] = 0;
  1193. s->sbm_phy_oldbmsr = 0;
  1194. s->sbm_phy_oldanlpar = 0;
  1195. s->sbm_phy_oldk1stsr = 0;
  1196. s->sbm_phy_oldlinkstat = 0;
  1197. /*
  1198. * Initialize the DMA channels. Right now, only one per MAC is used
  1199. * Note: Only do this _once_, as it allocates memory from the kernel!
  1200. */
  1201. sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
  1202. sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
  1203. /*
  1204. * initial state is OFF
  1205. */
  1206. s->sbm_state = sbmac_state_off;
  1207. /*
  1208. * Initial speed is (XXX TEMP) 10MBit/s HDX no FC
  1209. */
  1210. s->sbm_speed = sbmac_speed_10;
  1211. s->sbm_duplex = sbmac_duplex_half;
  1212. s->sbm_fc = sbmac_fc_disabled;
  1213. return 0;
  1214. }
  1215. static void sbdma_uninitctx(struct sbmacdma_s *d)
  1216. {
  1217. if (d->sbdma_dscrtable_unaligned) {
  1218. kfree(d->sbdma_dscrtable_unaligned);
  1219. d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
  1220. }
  1221. if (d->sbdma_ctxtable) {
  1222. kfree(d->sbdma_ctxtable);
  1223. d->sbdma_ctxtable = NULL;
  1224. }
  1225. }
  1226. static void sbmac_uninitctx(struct sbmac_softc *sc)
  1227. {
  1228. sbdma_uninitctx(&(sc->sbm_txdma));
  1229. sbdma_uninitctx(&(sc->sbm_rxdma));
  1230. }
  1231. /**********************************************************************
  1232. * SBMAC_CHANNEL_START(s)
  1233. *
  1234. * Start packet processing on this MAC.
  1235. *
  1236. * Input parameters:
  1237. * s - sbmac structure
  1238. *
  1239. * Return value:
  1240. * nothing
  1241. ********************************************************************* */
  1242. static void sbmac_channel_start(struct sbmac_softc *s)
  1243. {
  1244. uint64_t reg;
  1245. volatile void __iomem *port;
  1246. uint64_t cfg,fifo,framecfg;
  1247. int idx, th_value;
  1248. /*
  1249. * Don't do this if running
  1250. */
  1251. if (s->sbm_state == sbmac_state_on)
  1252. return;
  1253. /*
  1254. * Bring the controller out of reset, but leave it off.
  1255. */
  1256. __raw_writeq(0, s->sbm_macenable);
  1257. /*
  1258. * Ignore all received packets
  1259. */
  1260. __raw_writeq(0, s->sbm_rxfilter);
  1261. /*
  1262. * Calculate values for various control registers.
  1263. */
  1264. cfg = M_MAC_RETRY_EN |
  1265. M_MAC_TX_HOLD_SOP_EN |
  1266. V_MAC_TX_PAUSE_CNT_16K |
  1267. M_MAC_AP_STAT_EN |
  1268. M_MAC_FAST_SYNC |
  1269. M_MAC_SS_EN |
  1270. 0;
  1271. /*
  1272. * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
  1273. * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
  1274. * Use a larger RD_THRSH for gigabit
  1275. */
  1276. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
  1277. th_value = 28;
  1278. else
  1279. th_value = 64;
  1280. fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
  1281. ((s->sbm_speed == sbmac_speed_1000)
  1282. ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
  1283. V_MAC_TX_RL_THRSH(4) |
  1284. V_MAC_RX_PL_THRSH(4) |
  1285. V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
  1286. V_MAC_RX_PL_THRSH(4) |
  1287. V_MAC_RX_RL_THRSH(8) |
  1288. 0;
  1289. framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
  1290. V_MAC_MAX_FRAMESZ_DEFAULT |
  1291. V_MAC_BACKOFF_SEL(1);
  1292. /*
  1293. * Clear out the hash address map
  1294. */
  1295. port = s->sbm_base + R_MAC_HASH_BASE;
  1296. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1297. __raw_writeq(0, port);
  1298. port += sizeof(uint64_t);
  1299. }
  1300. /*
  1301. * Clear out the exact-match table
  1302. */
  1303. port = s->sbm_base + R_MAC_ADDR_BASE;
  1304. for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
  1305. __raw_writeq(0, port);
  1306. port += sizeof(uint64_t);
  1307. }
  1308. /*
  1309. * Clear out the DMA Channel mapping table registers
  1310. */
  1311. port = s->sbm_base + R_MAC_CHUP0_BASE;
  1312. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1313. __raw_writeq(0, port);
  1314. port += sizeof(uint64_t);
  1315. }
  1316. port = s->sbm_base + R_MAC_CHLO0_BASE;
  1317. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1318. __raw_writeq(0, port);
  1319. port += sizeof(uint64_t);
  1320. }
  1321. /*
  1322. * Program the hardware address. It goes into the hardware-address
  1323. * register as well as the first filter register.
  1324. */
  1325. reg = sbmac_addr2reg(s->sbm_hwaddr);
  1326. port = s->sbm_base + R_MAC_ADDR_BASE;
  1327. __raw_writeq(reg, port);
  1328. port = s->sbm_base + R_MAC_ETHERNET_ADDR;
  1329. #ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
  1330. /*
  1331. * Pass1 SOCs do not receive packets addressed to the
  1332. * destination address in the R_MAC_ETHERNET_ADDR register.
  1333. * Set the value to zero.
  1334. */
  1335. __raw_writeq(0, port);
  1336. #else
  1337. __raw_writeq(reg, port);
  1338. #endif
  1339. /*
  1340. * Set the receive filter for no packets, and write values
  1341. * to the various config registers
  1342. */
  1343. __raw_writeq(0, s->sbm_rxfilter);
  1344. __raw_writeq(0, s->sbm_imr);
  1345. __raw_writeq(framecfg, s->sbm_framecfg);
  1346. __raw_writeq(fifo, s->sbm_fifocfg);
  1347. __raw_writeq(cfg, s->sbm_maccfg);
  1348. /*
  1349. * Initialize DMA channels (rings should be ok now)
  1350. */
  1351. sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
  1352. sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
  1353. /*
  1354. * Configure the speed, duplex, and flow control
  1355. */
  1356. sbmac_set_speed(s,s->sbm_speed);
  1357. sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
  1358. /*
  1359. * Fill the receive ring
  1360. */
  1361. sbdma_fillring(&(s->sbm_rxdma));
  1362. /*
  1363. * Turn on the rest of the bits in the enable register
  1364. */
  1365. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  1366. __raw_writeq(M_MAC_RXDMA_EN0 |
  1367. M_MAC_TXDMA_EN0, s->sbm_macenable);
  1368. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  1369. __raw_writeq(M_MAC_RXDMA_EN0 |
  1370. M_MAC_TXDMA_EN0 |
  1371. M_MAC_RX_ENABLE |
  1372. M_MAC_TX_ENABLE, s->sbm_macenable);
  1373. #else
  1374. #error invalid SiByte MAC configuation
  1375. #endif
  1376. #ifdef CONFIG_SBMAC_COALESCE
  1377. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  1378. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
  1379. #else
  1380. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  1381. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
  1382. #endif
  1383. /*
  1384. * Enable receiving unicasts and broadcasts
  1385. */
  1386. __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
  1387. /*
  1388. * we're running now.
  1389. */
  1390. s->sbm_state = sbmac_state_on;
  1391. /*
  1392. * Program multicast addresses
  1393. */
  1394. sbmac_setmulti(s);
  1395. /*
  1396. * If channel was in promiscuous mode before, turn that on
  1397. */
  1398. if (s->sbm_devflags & IFF_PROMISC) {
  1399. sbmac_promiscuous_mode(s,1);
  1400. }
  1401. }
  1402. /**********************************************************************
  1403. * SBMAC_CHANNEL_STOP(s)
  1404. *
  1405. * Stop packet processing on this MAC.
  1406. *
  1407. * Input parameters:
  1408. * s - sbmac structure
  1409. *
  1410. * Return value:
  1411. * nothing
  1412. ********************************************************************* */
  1413. static void sbmac_channel_stop(struct sbmac_softc *s)
  1414. {
  1415. /* don't do this if already stopped */
  1416. if (s->sbm_state == sbmac_state_off)
  1417. return;
  1418. /* don't accept any packets, disable all interrupts */
  1419. __raw_writeq(0, s->sbm_rxfilter);
  1420. __raw_writeq(0, s->sbm_imr);
  1421. /* Turn off ticker */
  1422. /* XXX */
  1423. /* turn off receiver and transmitter */
  1424. __raw_writeq(0, s->sbm_macenable);
  1425. /* We're stopped now. */
  1426. s->sbm_state = sbmac_state_off;
  1427. /*
  1428. * Stop DMA channels (rings should be ok now)
  1429. */
  1430. sbdma_channel_stop(&(s->sbm_rxdma));
  1431. sbdma_channel_stop(&(s->sbm_txdma));
  1432. /* Empty the receive and transmit rings */
  1433. sbdma_emptyring(&(s->sbm_rxdma));
  1434. sbdma_emptyring(&(s->sbm_txdma));
  1435. }
  1436. /**********************************************************************
  1437. * SBMAC_SET_CHANNEL_STATE(state)
  1438. *
  1439. * Set the channel's state ON or OFF
  1440. *
  1441. * Input parameters:
  1442. * state - new state
  1443. *
  1444. * Return value:
  1445. * old state
  1446. ********************************************************************* */
  1447. static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *sc,
  1448. sbmac_state_t state)
  1449. {
  1450. sbmac_state_t oldstate = sc->sbm_state;
  1451. /*
  1452. * If same as previous state, return
  1453. */
  1454. if (state == oldstate) {
  1455. return oldstate;
  1456. }
  1457. /*
  1458. * If new state is ON, turn channel on
  1459. */
  1460. if (state == sbmac_state_on) {
  1461. sbmac_channel_start(sc);
  1462. }
  1463. else {
  1464. sbmac_channel_stop(sc);
  1465. }
  1466. /*
  1467. * Return previous state
  1468. */
  1469. return oldstate;
  1470. }
  1471. /**********************************************************************
  1472. * SBMAC_PROMISCUOUS_MODE(sc,onoff)
  1473. *
  1474. * Turn on or off promiscuous mode
  1475. *
  1476. * Input parameters:
  1477. * sc - softc
  1478. * onoff - 1 to turn on, 0 to turn off
  1479. *
  1480. * Return value:
  1481. * nothing
  1482. ********************************************************************* */
  1483. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
  1484. {
  1485. uint64_t reg;
  1486. if (sc->sbm_state != sbmac_state_on)
  1487. return;
  1488. if (onoff) {
  1489. reg = __raw_readq(sc->sbm_rxfilter);
  1490. reg |= M_MAC_ALLPKT_EN;
  1491. __raw_writeq(reg, sc->sbm_rxfilter);
  1492. }
  1493. else {
  1494. reg = __raw_readq(sc->sbm_rxfilter);
  1495. reg &= ~M_MAC_ALLPKT_EN;
  1496. __raw_writeq(reg, sc->sbm_rxfilter);
  1497. }
  1498. }
  1499. /**********************************************************************
  1500. * SBMAC_SETIPHDR_OFFSET(sc,onoff)
  1501. *
  1502. * Set the iphdr offset as 15 assuming ethernet encapsulation
  1503. *
  1504. * Input parameters:
  1505. * sc - softc
  1506. *
  1507. * Return value:
  1508. * nothing
  1509. ********************************************************************* */
  1510. static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
  1511. {
  1512. uint64_t reg;
  1513. /* Hard code the off set to 15 for now */
  1514. reg = __raw_readq(sc->sbm_rxfilter);
  1515. reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
  1516. __raw_writeq(reg, sc->sbm_rxfilter);
  1517. /* BCM1250 pass1 didn't have hardware checksum. Everything
  1518. later does. */
  1519. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
  1520. sc->rx_hw_checksum = DISABLE;
  1521. } else {
  1522. sc->rx_hw_checksum = ENABLE;
  1523. }
  1524. }
  1525. /**********************************************************************
  1526. * SBMAC_ADDR2REG(ptr)
  1527. *
  1528. * Convert six bytes into the 64-bit register value that
  1529. * we typically write into the SBMAC's address/mcast registers
  1530. *
  1531. * Input parameters:
  1532. * ptr - pointer to 6 bytes
  1533. *
  1534. * Return value:
  1535. * register value
  1536. ********************************************************************* */
  1537. static uint64_t sbmac_addr2reg(unsigned char *ptr)
  1538. {
  1539. uint64_t reg = 0;
  1540. ptr += 6;
  1541. reg |= (uint64_t) *(--ptr);
  1542. reg <<= 8;
  1543. reg |= (uint64_t) *(--ptr);
  1544. reg <<= 8;
  1545. reg |= (uint64_t) *(--ptr);
  1546. reg <<= 8;
  1547. reg |= (uint64_t) *(--ptr);
  1548. reg <<= 8;
  1549. reg |= (uint64_t) *(--ptr);
  1550. reg <<= 8;
  1551. reg |= (uint64_t) *(--ptr);
  1552. return reg;
  1553. }
  1554. /**********************************************************************
  1555. * SBMAC_SET_SPEED(s,speed)
  1556. *
  1557. * Configure LAN speed for the specified MAC.
  1558. * Warning: must be called when MAC is off!
  1559. *
  1560. * Input parameters:
  1561. * s - sbmac structure
  1562. * speed - speed to set MAC to (see sbmac_speed_t enum)
  1563. *
  1564. * Return value:
  1565. * 1 if successful
  1566. * 0 indicates invalid parameters
  1567. ********************************************************************* */
  1568. static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed)
  1569. {
  1570. uint64_t cfg;
  1571. uint64_t framecfg;
  1572. /*
  1573. * Save new current values
  1574. */
  1575. s->sbm_speed = speed;
  1576. if (s->sbm_state == sbmac_state_on)
  1577. return 0; /* save for next restart */
  1578. /*
  1579. * Read current register values
  1580. */
  1581. cfg = __raw_readq(s->sbm_maccfg);
  1582. framecfg = __raw_readq(s->sbm_framecfg);
  1583. /*
  1584. * Mask out the stuff we want to change
  1585. */
  1586. cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
  1587. framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
  1588. M_MAC_SLOT_SIZE);
  1589. /*
  1590. * Now add in the new bits
  1591. */
  1592. switch (speed) {
  1593. case sbmac_speed_10:
  1594. framecfg |= V_MAC_IFG_RX_10 |
  1595. V_MAC_IFG_TX_10 |
  1596. K_MAC_IFG_THRSH_10 |
  1597. V_MAC_SLOT_SIZE_10;
  1598. cfg |= V_MAC_SPEED_SEL_10MBPS;
  1599. break;
  1600. case sbmac_speed_100:
  1601. framecfg |= V_MAC_IFG_RX_100 |
  1602. V_MAC_IFG_TX_100 |
  1603. V_MAC_IFG_THRSH_100 |
  1604. V_MAC_SLOT_SIZE_100;
  1605. cfg |= V_MAC_SPEED_SEL_100MBPS ;
  1606. break;
  1607. case sbmac_speed_1000:
  1608. framecfg |= V_MAC_IFG_RX_1000 |
  1609. V_MAC_IFG_TX_1000 |
  1610. V_MAC_IFG_THRSH_1000 |
  1611. V_MAC_SLOT_SIZE_1000;
  1612. cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
  1613. break;
  1614. case sbmac_speed_auto: /* XXX not implemented */
  1615. /* fall through */
  1616. default:
  1617. return 0;
  1618. }
  1619. /*
  1620. * Send the bits back to the hardware
  1621. */
  1622. __raw_writeq(framecfg, s->sbm_framecfg);
  1623. __raw_writeq(cfg, s->sbm_maccfg);
  1624. return 1;
  1625. }
  1626. /**********************************************************************
  1627. * SBMAC_SET_DUPLEX(s,duplex,fc)
  1628. *
  1629. * Set Ethernet duplex and flow control options for this MAC
  1630. * Warning: must be called when MAC is off!
  1631. *
  1632. * Input parameters:
  1633. * s - sbmac structure
  1634. * duplex - duplex setting (see sbmac_duplex_t)
  1635. * fc - flow control setting (see sbmac_fc_t)
  1636. *
  1637. * Return value:
  1638. * 1 if ok
  1639. * 0 if an invalid parameter combination was specified
  1640. ********************************************************************* */
  1641. static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc)
  1642. {
  1643. uint64_t cfg;
  1644. /*
  1645. * Save new current values
  1646. */
  1647. s->sbm_duplex = duplex;
  1648. s->sbm_fc = fc;
  1649. if (s->sbm_state == sbmac_state_on)
  1650. return 0; /* save for next restart */
  1651. /*
  1652. * Read current register values
  1653. */
  1654. cfg = __raw_readq(s->sbm_maccfg);
  1655. /*
  1656. * Mask off the stuff we're about to change
  1657. */
  1658. cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
  1659. switch (duplex) {
  1660. case sbmac_duplex_half:
  1661. switch (fc) {
  1662. case sbmac_fc_disabled:
  1663. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
  1664. break;
  1665. case sbmac_fc_collision:
  1666. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
  1667. break;
  1668. case sbmac_fc_carrier:
  1669. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
  1670. break;
  1671. case sbmac_fc_auto: /* XXX not implemented */
  1672. /* fall through */
  1673. case sbmac_fc_frame: /* not valid in half duplex */
  1674. default: /* invalid selection */
  1675. return 0;
  1676. }
  1677. break;
  1678. case sbmac_duplex_full:
  1679. switch (fc) {
  1680. case sbmac_fc_disabled:
  1681. cfg |= V_MAC_FC_CMD_DISABLED;
  1682. break;
  1683. case sbmac_fc_frame:
  1684. cfg |= V_MAC_FC_CMD_ENABLED;
  1685. break;
  1686. case sbmac_fc_collision: /* not valid in full duplex */
  1687. case sbmac_fc_carrier: /* not valid in full duplex */
  1688. case sbmac_fc_auto: /* XXX not implemented */
  1689. /* fall through */
  1690. default:
  1691. return 0;
  1692. }
  1693. break;
  1694. case sbmac_duplex_auto:
  1695. /* XXX not implemented */
  1696. break;
  1697. }
  1698. /*
  1699. * Send the bits back to the hardware
  1700. */
  1701. __raw_writeq(cfg, s->sbm_maccfg);
  1702. return 1;
  1703. }
  1704. /**********************************************************************
  1705. * SBMAC_INTR()
  1706. *
  1707. * Interrupt handler for MAC interrupts
  1708. *
  1709. * Input parameters:
  1710. * MAC structure
  1711. *
  1712. * Return value:
  1713. * nothing
  1714. ********************************************************************* */
  1715. static irqreturn_t sbmac_intr(int irq,void *dev_instance)
  1716. {
  1717. struct net_device *dev = (struct net_device *) dev_instance;
  1718. struct sbmac_softc *sc = netdev_priv(dev);
  1719. uint64_t isr;
  1720. int handled = 0;
  1721. /*
  1722. * Read the ISR (this clears the bits in the real
  1723. * register, except for counter addr)
  1724. */
  1725. isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
  1726. if (isr == 0)
  1727. return IRQ_RETVAL(0);
  1728. handled = 1;
  1729. /*
  1730. * Transmits on channel 0
  1731. */
  1732. if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0)) {
  1733. sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
  1734. #ifdef CONFIG_NETPOLL_TRAP
  1735. if (netpoll_trap()) {
  1736. if (test_and_clear_bit(__LINK_STATE_XOFF, &dev->state))
  1737. __netif_schedule(dev);
  1738. }
  1739. #endif
  1740. }
  1741. if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
  1742. if (netif_rx_schedule_prep(dev)) {
  1743. __raw_writeq(0, sc->sbm_imr);
  1744. __netif_rx_schedule(dev);
  1745. /* Depend on the exit from poll to reenable intr */
  1746. }
  1747. else {
  1748. /* may leave some packets behind */
  1749. sbdma_rx_process(sc,&(sc->sbm_rxdma),
  1750. SBMAC_MAX_RXDESCR * 2, 0);
  1751. }
  1752. }
  1753. return IRQ_RETVAL(handled);
  1754. }
  1755. /**********************************************************************
  1756. * SBMAC_START_TX(skb,dev)
  1757. *
  1758. * Start output on the specified interface. Basically, we
  1759. * queue as many buffers as we can until the ring fills up, or
  1760. * we run off the end of the queue, whichever comes first.
  1761. *
  1762. * Input parameters:
  1763. *
  1764. *
  1765. * Return value:
  1766. * nothing
  1767. ********************************************************************* */
  1768. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
  1769. {
  1770. struct sbmac_softc *sc = netdev_priv(dev);
  1771. /* lock eth irq */
  1772. spin_lock_irq (&sc->sbm_lock);
  1773. /*
  1774. * Put the buffer on the transmit ring. If we
  1775. * don't have room, stop the queue.
  1776. */
  1777. if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
  1778. /* XXX save skb that we could not send */
  1779. netif_stop_queue(dev);
  1780. spin_unlock_irq(&sc->sbm_lock);
  1781. return 1;
  1782. }
  1783. dev->trans_start = jiffies;
  1784. spin_unlock_irq (&sc->sbm_lock);
  1785. return 0;
  1786. }
  1787. /**********************************************************************
  1788. * SBMAC_SETMULTI(sc)
  1789. *
  1790. * Reprogram the multicast table into the hardware, given
  1791. * the list of multicasts associated with the interface
  1792. * structure.
  1793. *
  1794. * Input parameters:
  1795. * sc - softc
  1796. *
  1797. * Return value:
  1798. * nothing
  1799. ********************************************************************* */
  1800. static void sbmac_setmulti(struct sbmac_softc *sc)
  1801. {
  1802. uint64_t reg;
  1803. volatile void __iomem *port;
  1804. int idx;
  1805. struct dev_mc_list *mclist;
  1806. struct net_device *dev = sc->sbm_dev;
  1807. /*
  1808. * Clear out entire multicast table. We do this by nuking
  1809. * the entire hash table and all the direct matches except
  1810. * the first one, which is used for our station address
  1811. */
  1812. for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
  1813. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
  1814. __raw_writeq(0, port);
  1815. }
  1816. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1817. port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
  1818. __raw_writeq(0, port);
  1819. }
  1820. /*
  1821. * Clear the filter to say we don't want any multicasts.
  1822. */
  1823. reg = __raw_readq(sc->sbm_rxfilter);
  1824. reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1825. __raw_writeq(reg, sc->sbm_rxfilter);
  1826. if (dev->flags & IFF_ALLMULTI) {
  1827. /*
  1828. * Enable ALL multicasts. Do this by inverting the
  1829. * multicast enable bit.
  1830. */
  1831. reg = __raw_readq(sc->sbm_rxfilter);
  1832. reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1833. __raw_writeq(reg, sc->sbm_rxfilter);
  1834. return;
  1835. }
  1836. /*
  1837. * Progam new multicast entries. For now, only use the
  1838. * perfect filter. In the future we'll need to use the
  1839. * hash filter if the perfect filter overflows
  1840. */
  1841. /* XXX only using perfect filter for now, need to use hash
  1842. * XXX if the table overflows */
  1843. idx = 1; /* skip station address */
  1844. mclist = dev->mc_list;
  1845. while (mclist && (idx < MAC_ADDR_COUNT)) {
  1846. reg = sbmac_addr2reg(mclist->dmi_addr);
  1847. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
  1848. __raw_writeq(reg, port);
  1849. idx++;
  1850. mclist = mclist->next;
  1851. }
  1852. /*
  1853. * Enable the "accept multicast bits" if we programmed at least one
  1854. * multicast.
  1855. */
  1856. if (idx > 1) {
  1857. reg = __raw_readq(sc->sbm_rxfilter);
  1858. reg |= M_MAC_MCAST_EN;
  1859. __raw_writeq(reg, sc->sbm_rxfilter);
  1860. }
  1861. }
  1862. #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
  1863. /**********************************************************************
  1864. * SBMAC_PARSE_XDIGIT(str)
  1865. *
  1866. * Parse a hex digit, returning its value
  1867. *
  1868. * Input parameters:
  1869. * str - character
  1870. *
  1871. * Return value:
  1872. * hex value, or -1 if invalid
  1873. ********************************************************************* */
  1874. static int sbmac_parse_xdigit(char str)
  1875. {
  1876. int digit;
  1877. if ((str >= '0') && (str <= '9'))
  1878. digit = str - '0';
  1879. else if ((str >= 'a') && (str <= 'f'))
  1880. digit = str - 'a' + 10;
  1881. else if ((str >= 'A') && (str <= 'F'))
  1882. digit = str - 'A' + 10;
  1883. else
  1884. return -1;
  1885. return digit;
  1886. }
  1887. /**********************************************************************
  1888. * SBMAC_PARSE_HWADDR(str,hwaddr)
  1889. *
  1890. * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
  1891. * Ethernet address.
  1892. *
  1893. * Input parameters:
  1894. * str - string
  1895. * hwaddr - pointer to hardware address
  1896. *
  1897. * Return value:
  1898. * 0 if ok, else -1
  1899. ********************************************************************* */
  1900. static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr)
  1901. {
  1902. int digit1,digit2;
  1903. int idx = 6;
  1904. while (*str && (idx > 0)) {
  1905. digit1 = sbmac_parse_xdigit(*str);
  1906. if (digit1 < 0)
  1907. return -1;
  1908. str++;
  1909. if (!*str)
  1910. return -1;
  1911. if ((*str == ':') || (*str == '-')) {
  1912. digit2 = digit1;
  1913. digit1 = 0;
  1914. }
  1915. else {
  1916. digit2 = sbmac_parse_xdigit(*str);
  1917. if (digit2 < 0)
  1918. return -1;
  1919. str++;
  1920. }
  1921. *hwaddr++ = (digit1 << 4) | digit2;
  1922. idx--;
  1923. if (*str == '-')
  1924. str++;
  1925. if (*str == ':')
  1926. str++;
  1927. }
  1928. return 0;
  1929. }
  1930. #endif
  1931. static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
  1932. {
  1933. if (new_mtu > ENET_PACKET_SIZE)
  1934. return -EINVAL;
  1935. _dev->mtu = new_mtu;
  1936. printk(KERN_INFO "changing the mtu to %d\n", new_mtu);
  1937. return 0;
  1938. }
  1939. /**********************************************************************
  1940. * SBMAC_INIT(dev)
  1941. *
  1942. * Attach routine - init hardware and hook ourselves into linux
  1943. *
  1944. * Input parameters:
  1945. * dev - net_device structure
  1946. *
  1947. * Return value:
  1948. * status
  1949. ********************************************************************* */
  1950. static int sbmac_init(struct net_device *dev, int idx)
  1951. {
  1952. struct sbmac_softc *sc;
  1953. unsigned char *eaddr;
  1954. uint64_t ea_reg;
  1955. int i;
  1956. int err;
  1957. sc = netdev_priv(dev);
  1958. /* Determine controller base address */
  1959. sc->sbm_base = IOADDR(dev->base_addr);
  1960. sc->sbm_dev = dev;
  1961. sc->sbe_idx = idx;
  1962. eaddr = sc->sbm_hwaddr;
  1963. /*
  1964. * Read the ethernet address. The firwmare left this programmed
  1965. * for us in the ethernet address register for each mac.
  1966. */
  1967. ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1968. __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1969. for (i = 0; i < 6; i++) {
  1970. eaddr[i] = (uint8_t) (ea_reg & 0xFF);
  1971. ea_reg >>= 8;
  1972. }
  1973. for (i = 0; i < 6; i++) {
  1974. dev->dev_addr[i] = eaddr[i];
  1975. }
  1976. /*
  1977. * Init packet size
  1978. */
  1979. sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;
  1980. /*
  1981. * Initialize context (get pointers to registers and stuff), then
  1982. * allocate the memory for the descriptor tables.
  1983. */
  1984. sbmac_initctx(sc);
  1985. /*
  1986. * Set up Linux device callins
  1987. */
  1988. spin_lock_init(&(sc->sbm_lock));
  1989. dev->open = sbmac_open;
  1990. dev->hard_start_xmit = sbmac_start_tx;
  1991. dev->stop = sbmac_close;
  1992. dev->get_stats = sbmac_get_stats;
  1993. dev->set_multicast_list = sbmac_set_rx_mode;
  1994. dev->do_ioctl = sbmac_mii_ioctl;
  1995. dev->tx_timeout = sbmac_tx_timeout;
  1996. dev->watchdog_timeo = TX_TIMEOUT;
  1997. dev->poll = sbmac_poll;
  1998. dev->weight = 16;
  1999. dev->change_mtu = sb1250_change_mtu;
  2000. #ifdef CONFIG_NET_POLL_CONTROLLER
  2001. dev->poll_controller = sbmac_netpoll;
  2002. #endif
  2003. /* This is needed for PASS2 for Rx H/W checksum feature */
  2004. sbmac_set_iphdr_offset(sc);
  2005. err = register_netdev(dev);
  2006. if (err)
  2007. goto out_uninit;
  2008. if (sc->rx_hw_checksum == ENABLE) {
  2009. printk(KERN_INFO "%s: enabling TCP rcv checksum\n",
  2010. sc->sbm_dev->name);
  2011. }
  2012. /*
  2013. * Display Ethernet address (this is called during the config
  2014. * process so we need to finish off the config message that
  2015. * was being displayed)
  2016. */
  2017. printk(KERN_INFO
  2018. "%s: SiByte Ethernet at 0x%08lX, address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  2019. dev->name, dev->base_addr,
  2020. eaddr[0],eaddr[1],eaddr[2],eaddr[3],eaddr[4],eaddr[5]);
  2021. return 0;
  2022. out_uninit:
  2023. sbmac_uninitctx(sc);
  2024. return err;
  2025. }
  2026. static int sbmac_open(struct net_device *dev)
  2027. {
  2028. struct sbmac_softc *sc = netdev_priv(dev);
  2029. if (debug > 1) {
  2030. printk(KERN_DEBUG "%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
  2031. }
  2032. /*
  2033. * map/route interrupt (clear status first, in case something
  2034. * weird is pending; we haven't initialized the mac registers
  2035. * yet)
  2036. */
  2037. __raw_readq(sc->sbm_isr);
  2038. if (request_irq(dev->irq, &sbmac_intr, IRQF_SHARED, dev->name, dev))
  2039. return -EBUSY;
  2040. /*
  2041. * Probe phy address
  2042. */
  2043. if(sbmac_mii_probe(dev) == -1) {
  2044. printk("%s: failed to probe PHY.\n", dev->name);
  2045. return -EINVAL;
  2046. }
  2047. /*
  2048. * Configure default speed
  2049. */
  2050. sbmac_mii_poll(sc,noisy_mii);
  2051. /*
  2052. * Turn on the channel
  2053. */
  2054. sbmac_set_channel_state(sc,sbmac_state_on);
  2055. /*
  2056. * XXX Station address is in dev->dev_addr
  2057. */
  2058. if (dev->if_port == 0)
  2059. dev->if_port = 0;
  2060. netif_start_queue(dev);
  2061. sbmac_set_rx_mode(dev);
  2062. /* Set the timer to check for link beat. */
  2063. init_timer(&sc->sbm_timer);
  2064. sc->sbm_timer.expires = jiffies + 2 * HZ/100;
  2065. sc->sbm_timer.data = (unsigned long)dev;
  2066. sc->sbm_timer.function = &sbmac_timer;
  2067. add_timer(&sc->sbm_timer);
  2068. return 0;
  2069. }
  2070. static int sbmac_mii_probe(struct net_device *dev)
  2071. {
  2072. int i;
  2073. struct sbmac_softc *s = netdev_priv(dev);
  2074. u16 bmsr, id1, id2;
  2075. u32 vendor, device;
  2076. for (i=1; i<31; i++) {
  2077. bmsr = sbmac_mii_read(s, i, MII_BMSR);
  2078. if (bmsr != 0) {
  2079. s->sbm_phys[0] = i;
  2080. id1 = sbmac_mii_read(s, i, MII_PHYIDR1);
  2081. id2 = sbmac_mii_read(s, i, MII_PHYIDR2);
  2082. vendor = ((u32)id1 << 6) | ((id2 >> 10) & 0x3f);
  2083. device = (id2 >> 4) & 0x3f;
  2084. printk(KERN_INFO "%s: found phy %d, vendor %06x part %02x\n",
  2085. dev->name, i, vendor, device);
  2086. return i;
  2087. }
  2088. }
  2089. return -1;
  2090. }
  2091. static int sbmac_mii_poll(struct sbmac_softc *s,int noisy)
  2092. {
  2093. int bmsr,bmcr,k1stsr,anlpar;
  2094. int chg;
  2095. char buffer[100];
  2096. char *p = buffer;
  2097. /* Read the mode status and mode control registers. */
  2098. bmsr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMSR);
  2099. bmcr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMCR);
  2100. /* get the link partner status */
  2101. anlpar = sbmac_mii_read(s,s->sbm_phys[0],MII_ANLPAR);
  2102. /* if supported, read the 1000baseT register */
  2103. if (bmsr & BMSR_1000BT_XSR) {
  2104. k1stsr = sbmac_mii_read(s,s->sbm_phys[0],MII_K1STSR);
  2105. }
  2106. else {
  2107. k1stsr = 0;
  2108. }
  2109. chg = 0;
  2110. if ((bmsr & BMSR_LINKSTAT) == 0) {
  2111. /*
  2112. * If link status is down, clear out old info so that when
  2113. * it comes back up it will force us to reconfigure speed
  2114. */
  2115. s->sbm_phy_oldbmsr = 0;
  2116. s->sbm_phy_oldanlpar = 0;
  2117. s->sbm_phy_oldk1stsr = 0;
  2118. return 0;
  2119. }
  2120. if ((s->sbm_phy_oldbmsr != bmsr) ||
  2121. (s->sbm_phy_oldanlpar != anlpar) ||
  2122. (s->sbm_phy_oldk1stsr != k1stsr)) {
  2123. if (debug > 1) {
  2124. printk(KERN_DEBUG "%s: bmsr:%x/%x anlpar:%x/%x k1stsr:%x/%x\n",
  2125. s->sbm_dev->name,
  2126. s->sbm_phy_oldbmsr,bmsr,
  2127. s->sbm_phy_oldanlpar,anlpar,
  2128. s->sbm_phy_oldk1stsr,k1stsr);
  2129. }
  2130. s->sbm_phy_oldbmsr = bmsr;
  2131. s->sbm_phy_oldanlpar = anlpar;
  2132. s->sbm_phy_oldk1stsr = k1stsr;
  2133. chg = 1;
  2134. }
  2135. if (chg == 0)
  2136. return 0;
  2137. p += sprintf(p,"Link speed: ");
  2138. if (k1stsr & K1STSR_LP1KFD) {
  2139. s->sbm_speed = sbmac_speed_1000;
  2140. s->sbm_duplex = sbmac_duplex_full;
  2141. s->sbm_fc = sbmac_fc_frame;
  2142. p += sprintf(p,"1000BaseT FDX");
  2143. }
  2144. else if (k1stsr & K1STSR_LP1KHD) {
  2145. s->sbm_speed = sbmac_speed_1000;
  2146. s->sbm_duplex = sbmac_duplex_half;
  2147. s->sbm_fc = sbmac_fc_disabled;
  2148. p += sprintf(p,"1000BaseT HDX");
  2149. }
  2150. else if (anlpar & ANLPAR_TXFD) {
  2151. s->sbm_speed = sbmac_speed_100;
  2152. s->sbm_duplex = sbmac_duplex_full;
  2153. s->sbm_fc = (anlpar & ANLPAR_PAUSE) ? sbmac_fc_frame : sbmac_fc_disabled;
  2154. p += sprintf(p,"100BaseT FDX");
  2155. }
  2156. else if (anlpar & ANLPAR_TXHD) {
  2157. s->sbm_speed = sbmac_speed_100;
  2158. s->sbm_duplex = sbmac_duplex_half;
  2159. s->sbm_fc = sbmac_fc_disabled;
  2160. p += sprintf(p,"100BaseT HDX");
  2161. }
  2162. else if (anlpar & ANLPAR_10FD) {
  2163. s->sbm_speed = sbmac_speed_10;
  2164. s->sbm_duplex = sbmac_duplex_full;
  2165. s->sbm_fc = sbmac_fc_frame;
  2166. p += sprintf(p,"10BaseT FDX");
  2167. }
  2168. else if (anlpar & ANLPAR_10HD) {
  2169. s->sbm_speed = sbmac_speed_10;
  2170. s->sbm_duplex = sbmac_duplex_half;
  2171. s->sbm_fc = sbmac_fc_collision;
  2172. p += sprintf(p,"10BaseT HDX");
  2173. }
  2174. else {
  2175. p += sprintf(p,"Unknown");
  2176. }
  2177. if (noisy) {
  2178. printk(KERN_INFO "%s: %s\n",s->sbm_dev->name,buffer);
  2179. }
  2180. return 1;
  2181. }
  2182. static void sbmac_timer(unsigned long data)
  2183. {
  2184. struct net_device *dev = (struct net_device *)data;
  2185. struct sbmac_softc *sc = netdev_priv(dev);
  2186. int next_tick = HZ;
  2187. int mii_status;
  2188. spin_lock_irq (&sc->sbm_lock);
  2189. /* make IFF_RUNNING follow the MII status bit "Link established" */
  2190. mii_status = sbmac_mii_read(sc, sc->sbm_phys[0], MII_BMSR);
  2191. if ( (mii_status & BMSR_LINKSTAT) != (sc->sbm_phy_oldlinkstat) ) {
  2192. sc->sbm_phy_oldlinkstat = mii_status & BMSR_LINKSTAT;
  2193. if (mii_status & BMSR_LINKSTAT) {
  2194. netif_carrier_on(dev);
  2195. }
  2196. else {
  2197. netif_carrier_off(dev);
  2198. }
  2199. }
  2200. /*
  2201. * Poll the PHY to see what speed we should be running at
  2202. */
  2203. if (sbmac_mii_poll(sc,noisy_mii)) {
  2204. if (sc->sbm_state != sbmac_state_off) {
  2205. /*
  2206. * something changed, restart the channel
  2207. */
  2208. if (debug > 1) {
  2209. printk("%s: restarting channel because speed changed\n",
  2210. sc->sbm_dev->name);
  2211. }
  2212. sbmac_channel_stop(sc);
  2213. sbmac_channel_start(sc);
  2214. }
  2215. }
  2216. spin_unlock_irq (&sc->sbm_lock);
  2217. sc->sbm_timer.expires = jiffies + next_tick;
  2218. add_timer(&sc->sbm_timer);
  2219. }
  2220. static void sbmac_tx_timeout (struct net_device *dev)
  2221. {
  2222. struct sbmac_softc *sc = netdev_priv(dev);
  2223. spin_lock_irq (&sc->sbm_lock);
  2224. dev->trans_start = jiffies;
  2225. sc->sbm_stats.tx_errors++;
  2226. spin_unlock_irq (&sc->sbm_lock);
  2227. printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
  2228. }
  2229. static struct net_device_stats *sbmac_get_stats(struct net_device *dev)
  2230. {
  2231. struct sbmac_softc *sc = netdev_priv(dev);
  2232. unsigned long flags;
  2233. spin_lock_irqsave(&sc->sbm_lock, flags);
  2234. /* XXX update other stats here */
  2235. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2236. return &sc->sbm_stats;
  2237. }
  2238. static void sbmac_set_rx_mode(struct net_device *dev)
  2239. {
  2240. unsigned long flags;
  2241. struct sbmac_softc *sc = netdev_priv(dev);
  2242. spin_lock_irqsave(&sc->sbm_lock, flags);
  2243. if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
  2244. /*
  2245. * Promiscuous changed.
  2246. */
  2247. if (dev->flags & IFF_PROMISC) {
  2248. sbmac_promiscuous_mode(sc,1);
  2249. }
  2250. else {
  2251. sbmac_promiscuous_mode(sc,0);
  2252. }
  2253. }
  2254. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2255. /*
  2256. * Program the multicasts. Do this every time.
  2257. */
  2258. sbmac_setmulti(sc);
  2259. }
  2260. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2261. {
  2262. struct sbmac_softc *sc = netdev_priv(dev);
  2263. u16 *data = (u16 *)&rq->ifr_ifru;
  2264. unsigned long flags;
  2265. int retval;
  2266. spin_lock_irqsave(&sc->sbm_lock, flags);
  2267. retval = 0;
  2268. switch(cmd) {
  2269. case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
  2270. data[0] = sc->sbm_phys[0] & 0x1f;
  2271. /* Fall Through */
  2272. case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
  2273. data[3] = sbmac_mii_read(sc, data[0] & 0x1f, data[1] & 0x1f);
  2274. break;
  2275. case SIOCDEVPRIVATE+2: /* Write the specified MII register */
  2276. if (!capable(CAP_NET_ADMIN)) {
  2277. retval = -EPERM;
  2278. break;
  2279. }
  2280. if (debug > 1) {
  2281. printk(KERN_DEBUG "%s: sbmac_mii_ioctl: write %02X %02X %02X\n",dev->name,
  2282. data[0],data[1],data[2]);
  2283. }
  2284. sbmac_mii_write(sc, data[0] & 0x1f, data[1] & 0x1f, data[2]);
  2285. break;
  2286. default:
  2287. retval = -EOPNOTSUPP;
  2288. }
  2289. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2290. return retval;
  2291. }
  2292. static int sbmac_close(struct net_device *dev)
  2293. {
  2294. struct sbmac_softc *sc = netdev_priv(dev);
  2295. unsigned long flags;
  2296. int irq;
  2297. sbmac_set_channel_state(sc,sbmac_state_off);
  2298. del_timer_sync(&sc->sbm_timer);
  2299. spin_lock_irqsave(&sc->sbm_lock, flags);
  2300. netif_stop_queue(dev);
  2301. if (debug > 1) {
  2302. printk(KERN_DEBUG "%s: Shutting down ethercard\n",dev->name);
  2303. }
  2304. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2305. irq = dev->irq;
  2306. synchronize_irq(irq);
  2307. free_irq(irq, dev);
  2308. sbdma_emptyring(&(sc->sbm_txdma));
  2309. sbdma_emptyring(&(sc->sbm_rxdma));
  2310. return 0;
  2311. }
  2312. static int sbmac_poll(struct net_device *dev, int *budget)
  2313. {
  2314. int work_to_do;
  2315. int work_done;
  2316. struct sbmac_softc *sc = netdev_priv(dev);
  2317. work_to_do = min(*budget, dev->quota);
  2318. work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), work_to_do, 1);
  2319. if (work_done > work_to_do)
  2320. printk(KERN_ERR "%s exceeded work_to_do budget=%d quota=%d work-done=%d\n",
  2321. sc->sbm_dev->name, *budget, dev->quota, work_done);
  2322. sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
  2323. *budget -= work_done;
  2324. dev->quota -= work_done;
  2325. if (work_done < work_to_do) {
  2326. netif_rx_complete(dev);
  2327. #ifdef CONFIG_SBMAC_COALESCE
  2328. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  2329. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  2330. sc->sbm_imr);
  2331. #else
  2332. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  2333. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  2334. #endif
  2335. }
  2336. return (work_done >= work_to_do);
  2337. }
  2338. #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
  2339. static void
  2340. sbmac_setup_hwaddr(int chan,char *addr)
  2341. {
  2342. uint8_t eaddr[6];
  2343. uint64_t val;
  2344. unsigned long port;
  2345. port = A_MAC_CHANNEL_BASE(chan);
  2346. sbmac_parse_hwaddr(addr,eaddr);
  2347. val = sbmac_addr2reg(eaddr);
  2348. __raw_writeq(val, IOADDR(port+R_MAC_ETHERNET_ADDR));
  2349. val = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
  2350. }
  2351. #endif
  2352. static struct net_device *dev_sbmac[MAX_UNITS];
  2353. static int __init
  2354. sbmac_init_module(void)
  2355. {
  2356. int idx;
  2357. struct net_device *dev;
  2358. unsigned long port;
  2359. int chip_max_units;
  2360. /* Set the number of available units based on the SOC type. */
  2361. switch (soc_type) {
  2362. case K_SYS_SOC_TYPE_BCM1250:
  2363. case K_SYS_SOC_TYPE_BCM1250_ALT:
  2364. chip_max_units = 3;
  2365. break;
  2366. case K_SYS_SOC_TYPE_BCM1120:
  2367. case K_SYS_SOC_TYPE_BCM1125:
  2368. case K_SYS_SOC_TYPE_BCM1125H:
  2369. case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
  2370. chip_max_units = 2;
  2371. break;
  2372. case K_SYS_SOC_TYPE_BCM1x55:
  2373. case K_SYS_SOC_TYPE_BCM1x80:
  2374. chip_max_units = 4;
  2375. break;
  2376. default:
  2377. chip_max_units = 0;
  2378. break;
  2379. }
  2380. if (chip_max_units > MAX_UNITS)
  2381. chip_max_units = MAX_UNITS;
  2382. /*
  2383. * For bringup when not using the firmware, we can pre-fill
  2384. * the MAC addresses using the environment variables
  2385. * specified in this file (or maybe from the config file?)
  2386. */
  2387. #ifdef SBMAC_ETH0_HWADDR
  2388. if (chip_max_units > 0)
  2389. sbmac_setup_hwaddr(0,SBMAC_ETH0_HWADDR);
  2390. #endif
  2391. #ifdef SBMAC_ETH1_HWADDR
  2392. if (chip_max_units > 1)
  2393. sbmac_setup_hwaddr(1,SBMAC_ETH1_HWADDR);
  2394. #endif
  2395. #ifdef SBMAC_ETH2_HWADDR
  2396. if (chip_max_units > 2)
  2397. sbmac_setup_hwaddr(2,SBMAC_ETH2_HWADDR);
  2398. #endif
  2399. #ifdef SBMAC_ETH3_HWADDR
  2400. if (chip_max_units > 3)
  2401. sbmac_setup_hwaddr(3,SBMAC_ETH3_HWADDR);
  2402. #endif
  2403. /*
  2404. * Walk through the Ethernet controllers and find
  2405. * those who have their MAC addresses set.
  2406. */
  2407. for (idx = 0; idx < chip_max_units; idx++) {
  2408. /*
  2409. * This is the base address of the MAC.
  2410. */
  2411. port = A_MAC_CHANNEL_BASE(idx);
  2412. /*
  2413. * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
  2414. * value for us by the firmware if we are going to use this MAC.
  2415. * If we find a zero, skip this MAC.
  2416. */
  2417. sbmac_orig_hwaddr[idx] = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
  2418. if (sbmac_orig_hwaddr[idx] == 0) {
  2419. printk(KERN_DEBUG "sbmac: not configuring MAC at "
  2420. "%lx\n", port);
  2421. continue;
  2422. }
  2423. /*
  2424. * Okay, cool. Initialize this MAC.
  2425. */
  2426. dev = alloc_etherdev(sizeof(struct sbmac_softc));
  2427. if (!dev)
  2428. return -ENOMEM;
  2429. printk(KERN_DEBUG "sbmac: configuring MAC at %lx\n", port);
  2430. dev->irq = UNIT_INT(idx);
  2431. dev->base_addr = port;
  2432. dev->mem_end = 0;
  2433. if (sbmac_init(dev, idx)) {
  2434. port = A_MAC_CHANNEL_BASE(idx);
  2435. __raw_writeq(sbmac_orig_hwaddr[idx], IOADDR(port+R_MAC_ETHERNET_ADDR));
  2436. free_netdev(dev);
  2437. continue;
  2438. }
  2439. dev_sbmac[idx] = dev;
  2440. }
  2441. return 0;
  2442. }
  2443. static void __exit
  2444. sbmac_cleanup_module(void)
  2445. {
  2446. struct net_device *dev;
  2447. int idx;
  2448. for (idx = 0; idx < MAX_UNITS; idx++) {
  2449. struct sbmac_softc *sc;
  2450. dev = dev_sbmac[idx];
  2451. if (!dev)
  2452. continue;
  2453. sc = netdev_priv(dev);
  2454. unregister_netdev(dev);
  2455. sbmac_uninitctx(sc);
  2456. free_netdev(dev);
  2457. }
  2458. }
  2459. module_init(sbmac_init_module);
  2460. module_exit(sbmac_cleanup_module);