sb1250-mac.c 70 KB

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