sb1250-mac.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641
  1. /*
  2. * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
  3. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  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. * Updated to the driver model and the PHY abstraction layer
  23. * by Maciej W. Rozycki.
  24. */
  25. #include <linux/bug.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/string.h>
  29. #include <linux/timer.h>
  30. #include <linux/errno.h>
  31. #include <linux/ioport.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/bitops.h>
  38. #include <linux/err.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/mii.h>
  41. #include <linux/phy.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/prefetch.h>
  44. #include <asm/cache.h>
  45. #include <asm/io.h>
  46. #include <asm/processor.h> /* Processor type for cache alignment. */
  47. /* Operational parameters that usually are not changed. */
  48. #define CONFIG_SBMAC_COALESCE
  49. /* Time in jiffies before concluding the transmitter is hung. */
  50. #define TX_TIMEOUT (2*HZ)
  51. MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
  52. MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
  53. /* A few user-configurable values which may be modified when a driver
  54. module is loaded. */
  55. /* 1 normal messages, 0 quiet .. 7 verbose. */
  56. static int debug = 1;
  57. module_param(debug, int, 0444);
  58. MODULE_PARM_DESC(debug, "Debug messages");
  59. #ifdef CONFIG_SBMAC_COALESCE
  60. static int int_pktcnt_tx = 255;
  61. module_param(int_pktcnt_tx, int, 0444);
  62. MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
  63. static int int_timeout_tx = 255;
  64. module_param(int_timeout_tx, int, 0444);
  65. MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
  66. static int int_pktcnt_rx = 64;
  67. module_param(int_pktcnt_rx, int, 0444);
  68. MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
  69. static int int_timeout_rx = 64;
  70. module_param(int_timeout_rx, int, 0444);
  71. MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
  72. #endif
  73. #include <asm/sibyte/board.h>
  74. #include <asm/sibyte/sb1250.h>
  75. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  76. #include <asm/sibyte/bcm1480_regs.h>
  77. #include <asm/sibyte/bcm1480_int.h>
  78. #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
  79. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  80. #include <asm/sibyte/sb1250_regs.h>
  81. #include <asm/sibyte/sb1250_int.h>
  82. #else
  83. #error invalid SiByte MAC configuration
  84. #endif
  85. #include <asm/sibyte/sb1250_scd.h>
  86. #include <asm/sibyte/sb1250_mac.h>
  87. #include <asm/sibyte/sb1250_dma.h>
  88. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  89. #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
  90. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  91. #define UNIT_INT(n) (K_INT_MAC_0 + (n))
  92. #else
  93. #error invalid SiByte MAC configuration
  94. #endif
  95. #ifdef K_INT_PHY
  96. #define SBMAC_PHY_INT K_INT_PHY
  97. #else
  98. #define SBMAC_PHY_INT PHY_POLL
  99. #endif
  100. /**********************************************************************
  101. * Simple types
  102. ********************************************************************* */
  103. enum sbmac_speed {
  104. sbmac_speed_none = 0,
  105. sbmac_speed_10 = SPEED_10,
  106. sbmac_speed_100 = SPEED_100,
  107. sbmac_speed_1000 = SPEED_1000,
  108. };
  109. enum sbmac_duplex {
  110. sbmac_duplex_none = -1,
  111. sbmac_duplex_half = DUPLEX_HALF,
  112. sbmac_duplex_full = DUPLEX_FULL,
  113. };
  114. enum sbmac_fc {
  115. sbmac_fc_none,
  116. sbmac_fc_disabled,
  117. sbmac_fc_frame,
  118. sbmac_fc_collision,
  119. sbmac_fc_carrier,
  120. };
  121. enum sbmac_state {
  122. sbmac_state_uninit,
  123. sbmac_state_off,
  124. sbmac_state_on,
  125. sbmac_state_broken,
  126. };
  127. /**********************************************************************
  128. * Macros
  129. ********************************************************************* */
  130. #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
  131. (d)->sbdma_dscrtable : (d)->f+1)
  132. #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
  133. #define SBMAC_MAX_TXDESCR 256
  134. #define SBMAC_MAX_RXDESCR 256
  135. #define ENET_PACKET_SIZE 1518
  136. /*#define ENET_PACKET_SIZE 9216 */
  137. /**********************************************************************
  138. * DMA Descriptor structure
  139. ********************************************************************* */
  140. struct sbdmadscr {
  141. uint64_t dscr_a;
  142. uint64_t dscr_b;
  143. };
  144. /**********************************************************************
  145. * DMA Controller structure
  146. ********************************************************************* */
  147. struct sbmacdma {
  148. /*
  149. * This stuff is used to identify the channel and the registers
  150. * associated with it.
  151. */
  152. struct sbmac_softc *sbdma_eth; /* back pointer to associated
  153. MAC */
  154. int sbdma_channel; /* channel number */
  155. int sbdma_txdir; /* direction (1=transmit) */
  156. int sbdma_maxdescr; /* total # of descriptors
  157. in ring */
  158. #ifdef CONFIG_SBMAC_COALESCE
  159. int sbdma_int_pktcnt;
  160. /* # descriptors rx/tx
  161. before interrupt */
  162. int sbdma_int_timeout;
  163. /* # usec rx/tx interrupt */
  164. #endif
  165. void __iomem *sbdma_config0; /* DMA config register 0 */
  166. void __iomem *sbdma_config1; /* DMA config register 1 */
  167. void __iomem *sbdma_dscrbase;
  168. /* descriptor base address */
  169. void __iomem *sbdma_dscrcnt; /* descriptor count register */
  170. void __iomem *sbdma_curdscr; /* current descriptor
  171. address */
  172. void __iomem *sbdma_oodpktlost;
  173. /* pkt drop (rx only) */
  174. /*
  175. * This stuff is for maintenance of the ring
  176. */
  177. void *sbdma_dscrtable_unaligned;
  178. struct sbdmadscr *sbdma_dscrtable;
  179. /* base of descriptor table */
  180. struct sbdmadscr *sbdma_dscrtable_end;
  181. /* end of descriptor table */
  182. struct sk_buff **sbdma_ctxtable;
  183. /* context table, one
  184. per descr */
  185. dma_addr_t sbdma_dscrtable_phys;
  186. /* and also the phys addr */
  187. struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
  188. struct sbdmadscr *sbdma_remptr; /* next dscr for sw
  189. to remove */
  190. };
  191. /**********************************************************************
  192. * Ethernet softc structure
  193. ********************************************************************* */
  194. struct sbmac_softc {
  195. /*
  196. * Linux-specific things
  197. */
  198. struct net_device *sbm_dev; /* pointer to linux device */
  199. struct napi_struct napi;
  200. struct phy_device *phy_dev; /* the associated PHY device */
  201. struct mii_bus *mii_bus; /* the MII bus */
  202. spinlock_t sbm_lock; /* spin lock */
  203. int sbm_devflags; /* current device flags */
  204. /*
  205. * Controller-specific things
  206. */
  207. void __iomem *sbm_base; /* MAC's base address */
  208. enum sbmac_state sbm_state; /* current state */
  209. void __iomem *sbm_macenable; /* MAC Enable Register */
  210. void __iomem *sbm_maccfg; /* MAC Config Register */
  211. void __iomem *sbm_fifocfg; /* FIFO Config Register */
  212. void __iomem *sbm_framecfg; /* Frame Config Register */
  213. void __iomem *sbm_rxfilter; /* Receive Filter Register */
  214. void __iomem *sbm_isr; /* Interrupt Status Register */
  215. void __iomem *sbm_imr; /* Interrupt Mask Register */
  216. void __iomem *sbm_mdio; /* MDIO Register */
  217. enum sbmac_speed sbm_speed; /* current speed */
  218. enum sbmac_duplex sbm_duplex; /* current duplex */
  219. enum sbmac_fc sbm_fc; /* cur. flow control setting */
  220. int sbm_pause; /* current pause setting */
  221. int sbm_link; /* current link state */
  222. unsigned char sbm_hwaddr[ETH_ALEN];
  223. struct sbmacdma sbm_txdma; /* only channel 0 for now */
  224. struct sbmacdma sbm_rxdma;
  225. int rx_hw_checksum;
  226. int sbe_idx;
  227. };
  228. /**********************************************************************
  229. * Externs
  230. ********************************************************************* */
  231. /**********************************************************************
  232. * Prototypes
  233. ********************************************************************* */
  234. static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
  235. int txrx, int maxdescr);
  236. static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
  237. static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
  238. struct sk_buff *m);
  239. static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
  240. static void sbdma_emptyring(struct sbmacdma *d);
  241. static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
  242. static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  243. int work_to_do, int poll);
  244. static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  245. int poll);
  246. static int sbmac_initctx(struct sbmac_softc *s);
  247. static void sbmac_channel_start(struct sbmac_softc *s);
  248. static void sbmac_channel_stop(struct sbmac_softc *s);
  249. static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
  250. enum sbmac_state);
  251. static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
  252. static uint64_t sbmac_addr2reg(unsigned char *ptr);
  253. static irqreturn_t sbmac_intr(int irq, void *dev_instance);
  254. static netdev_tx_t sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
  255. static void sbmac_setmulti(struct sbmac_softc *sc);
  256. static int sbmac_init(struct platform_device *pldev, long long base);
  257. static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
  258. static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
  259. enum sbmac_fc fc);
  260. static int sbmac_open(struct net_device *dev);
  261. static void sbmac_tx_timeout (struct net_device *dev);
  262. static void sbmac_set_rx_mode(struct net_device *dev);
  263. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  264. static int sbmac_close(struct net_device *dev);
  265. static int sbmac_poll(struct napi_struct *napi, int budget);
  266. static void sbmac_mii_poll(struct net_device *dev);
  267. static int sbmac_mii_probe(struct net_device *dev);
  268. static void sbmac_mii_sync(void __iomem *sbm_mdio);
  269. static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
  270. int bitcnt);
  271. static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
  272. static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  273. u16 val);
  274. /**********************************************************************
  275. * Globals
  276. ********************************************************************* */
  277. static char sbmac_string[] = "sb1250-mac";
  278. static char sbmac_mdio_string[] = "sb1250-mac-mdio";
  279. /**********************************************************************
  280. * MDIO constants
  281. ********************************************************************* */
  282. #define MII_COMMAND_START 0x01
  283. #define MII_COMMAND_READ 0x02
  284. #define MII_COMMAND_WRITE 0x01
  285. #define MII_COMMAND_ACK 0x02
  286. #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
  287. #define ENABLE 1
  288. #define DISABLE 0
  289. /**********************************************************************
  290. * SBMAC_MII_SYNC(sbm_mdio)
  291. *
  292. * Synchronize with the MII - send a pattern of bits to the MII
  293. * that will guarantee that it is ready to accept a command.
  294. *
  295. * Input parameters:
  296. * sbm_mdio - address of the MAC's MDIO register
  297. *
  298. * Return value:
  299. * nothing
  300. ********************************************************************* */
  301. static void sbmac_mii_sync(void __iomem *sbm_mdio)
  302. {
  303. int cnt;
  304. uint64_t bits;
  305. int mac_mdio_genc;
  306. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  307. bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
  308. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  309. for (cnt = 0; cnt < 32; cnt++) {
  310. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
  311. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  312. }
  313. }
  314. /**********************************************************************
  315. * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
  316. *
  317. * Send some bits to the MII. The bits to be sent are right-
  318. * justified in the 'data' parameter.
  319. *
  320. * Input parameters:
  321. * sbm_mdio - address of the MAC's MDIO register
  322. * data - data to send
  323. * bitcnt - number of bits to send
  324. ********************************************************************* */
  325. static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
  326. int bitcnt)
  327. {
  328. int i;
  329. uint64_t bits;
  330. unsigned int curmask;
  331. int mac_mdio_genc;
  332. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  333. bits = M_MAC_MDIO_DIR_OUTPUT;
  334. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  335. curmask = 1 << (bitcnt - 1);
  336. for (i = 0; i < bitcnt; i++) {
  337. if (data & curmask)
  338. bits |= M_MAC_MDIO_OUT;
  339. else bits &= ~M_MAC_MDIO_OUT;
  340. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  341. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
  342. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  343. curmask >>= 1;
  344. }
  345. }
  346. /**********************************************************************
  347. * SBMAC_MII_READ(bus, phyaddr, regidx)
  348. * Read a PHY register.
  349. *
  350. * Input parameters:
  351. * bus - MDIO bus handle
  352. * phyaddr - PHY's address
  353. * regnum - index of register to read
  354. *
  355. * Return value:
  356. * value read, or 0xffff if an error occurred.
  357. ********************************************************************* */
  358. static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
  359. {
  360. struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
  361. void __iomem *sbm_mdio = sc->sbm_mdio;
  362. int idx;
  363. int error;
  364. int regval;
  365. int mac_mdio_genc;
  366. /*
  367. * Synchronize ourselves so that the PHY knows the next
  368. * thing coming down is a command
  369. */
  370. sbmac_mii_sync(sbm_mdio);
  371. /*
  372. * Send the data to the PHY. The sequence is
  373. * a "start" command (2 bits)
  374. * a "read" command (2 bits)
  375. * the PHY addr (5 bits)
  376. * the register index (5 bits)
  377. */
  378. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
  379. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
  380. sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
  381. sbmac_mii_senddata(sbm_mdio, regidx, 5);
  382. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  383. /*
  384. * Switch the port around without a clock transition.
  385. */
  386. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  387. /*
  388. * Send out a clock pulse to signal we want the status
  389. */
  390. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  391. sbm_mdio);
  392. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  393. /*
  394. * If an error occurred, the PHY will signal '1' back
  395. */
  396. error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
  397. /*
  398. * Issue an 'idle' clock pulse, but keep the direction
  399. * the same.
  400. */
  401. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  402. sbm_mdio);
  403. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  404. regval = 0;
  405. for (idx = 0; idx < 16; idx++) {
  406. regval <<= 1;
  407. if (error == 0) {
  408. if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
  409. regval |= 1;
  410. }
  411. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  412. sbm_mdio);
  413. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  414. }
  415. /* Switch back to output */
  416. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
  417. if (error == 0)
  418. return regval;
  419. return 0xffff;
  420. }
  421. /**********************************************************************
  422. * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
  423. *
  424. * Write a value to a PHY register.
  425. *
  426. * Input parameters:
  427. * bus - MDIO bus handle
  428. * phyaddr - PHY to use
  429. * regidx - register within the PHY
  430. * regval - data to write to register
  431. *
  432. * Return value:
  433. * 0 for success
  434. ********************************************************************* */
  435. static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  436. u16 regval)
  437. {
  438. struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
  439. void __iomem *sbm_mdio = sc->sbm_mdio;
  440. int mac_mdio_genc;
  441. sbmac_mii_sync(sbm_mdio);
  442. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
  443. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
  444. sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
  445. sbmac_mii_senddata(sbm_mdio, regidx, 5);
  446. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
  447. sbmac_mii_senddata(sbm_mdio, regval, 16);
  448. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  449. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
  450. return 0;
  451. }
  452. /**********************************************************************
  453. * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
  454. *
  455. * Initialize a DMA channel context. Since there are potentially
  456. * eight DMA channels per MAC, it's nice to do this in a standard
  457. * way.
  458. *
  459. * Input parameters:
  460. * d - struct sbmacdma (DMA channel context)
  461. * s - struct sbmac_softc (pointer to a MAC)
  462. * chan - channel number (0..1 right now)
  463. * txrx - Identifies DMA_TX or DMA_RX for channel direction
  464. * maxdescr - number of descriptors
  465. *
  466. * Return value:
  467. * nothing
  468. ********************************************************************* */
  469. static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
  470. int txrx, int maxdescr)
  471. {
  472. #ifdef CONFIG_SBMAC_COALESCE
  473. int int_pktcnt, int_timeout;
  474. #endif
  475. /*
  476. * Save away interesting stuff in the structure
  477. */
  478. d->sbdma_eth = s;
  479. d->sbdma_channel = chan;
  480. d->sbdma_txdir = txrx;
  481. #if 0
  482. /* RMON clearing */
  483. s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
  484. #endif
  485. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
  486. __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
  487. __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
  488. __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
  489. __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
  490. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
  491. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
  492. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
  493. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
  494. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
  495. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
  496. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
  497. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
  498. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
  499. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
  500. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
  501. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
  502. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
  503. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
  504. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
  505. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
  506. /*
  507. * initialize register pointers
  508. */
  509. d->sbdma_config0 =
  510. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
  511. d->sbdma_config1 =
  512. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
  513. d->sbdma_dscrbase =
  514. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
  515. d->sbdma_dscrcnt =
  516. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
  517. d->sbdma_curdscr =
  518. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
  519. if (d->sbdma_txdir)
  520. d->sbdma_oodpktlost = NULL;
  521. else
  522. d->sbdma_oodpktlost =
  523. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
  524. /*
  525. * Allocate memory for the ring
  526. */
  527. d->sbdma_maxdescr = maxdescr;
  528. d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
  529. sizeof(*d->sbdma_dscrtable),
  530. GFP_KERNEL);
  531. /*
  532. * The descriptor table must be aligned to at least 16 bytes or the
  533. * MAC will corrupt it.
  534. */
  535. d->sbdma_dscrtable = (struct sbdmadscr *)
  536. ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
  537. sizeof(*d->sbdma_dscrtable));
  538. d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
  539. d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
  540. /*
  541. * And context table
  542. */
  543. d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
  544. sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
  545. #ifdef CONFIG_SBMAC_COALESCE
  546. /*
  547. * Setup Rx/Tx DMA coalescing defaults
  548. */
  549. int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
  550. if ( int_pktcnt ) {
  551. d->sbdma_int_pktcnt = int_pktcnt;
  552. } else {
  553. d->sbdma_int_pktcnt = 1;
  554. }
  555. int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
  556. if ( int_timeout ) {
  557. d->sbdma_int_timeout = int_timeout;
  558. } else {
  559. d->sbdma_int_timeout = 0;
  560. }
  561. #endif
  562. }
  563. /**********************************************************************
  564. * SBDMA_CHANNEL_START(d)
  565. *
  566. * Initialize the hardware registers for a DMA channel.
  567. *
  568. * Input parameters:
  569. * d - DMA channel to init (context must be previously init'd
  570. * rxtx - DMA_RX or DMA_TX depending on what type of channel
  571. *
  572. * Return value:
  573. * nothing
  574. ********************************************************************* */
  575. static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
  576. {
  577. /*
  578. * Turn on the DMA channel
  579. */
  580. #ifdef CONFIG_SBMAC_COALESCE
  581. __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
  582. 0, d->sbdma_config1);
  583. __raw_writeq(M_DMA_EOP_INT_EN |
  584. V_DMA_RINGSZ(d->sbdma_maxdescr) |
  585. V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
  586. 0, d->sbdma_config0);
  587. #else
  588. __raw_writeq(0, d->sbdma_config1);
  589. __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
  590. 0, d->sbdma_config0);
  591. #endif
  592. __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
  593. /*
  594. * Initialize ring pointers
  595. */
  596. d->sbdma_addptr = d->sbdma_dscrtable;
  597. d->sbdma_remptr = d->sbdma_dscrtable;
  598. }
  599. /**********************************************************************
  600. * SBDMA_CHANNEL_STOP(d)
  601. *
  602. * Initialize the hardware registers for a DMA channel.
  603. *
  604. * Input parameters:
  605. * d - DMA channel to init (context must be previously init'd
  606. *
  607. * Return value:
  608. * nothing
  609. ********************************************************************* */
  610. static void sbdma_channel_stop(struct sbmacdma *d)
  611. {
  612. /*
  613. * Turn off the DMA channel
  614. */
  615. __raw_writeq(0, d->sbdma_config1);
  616. __raw_writeq(0, d->sbdma_dscrbase);
  617. __raw_writeq(0, d->sbdma_config0);
  618. /*
  619. * Zero ring pointers
  620. */
  621. d->sbdma_addptr = NULL;
  622. d->sbdma_remptr = NULL;
  623. }
  624. static inline void sbdma_align_skb(struct sk_buff *skb,
  625. unsigned int power2, unsigned int offset)
  626. {
  627. unsigned char *addr = skb->data;
  628. unsigned char *newaddr = PTR_ALIGN(addr, power2);
  629. skb_reserve(skb, newaddr - addr + offset);
  630. }
  631. /**********************************************************************
  632. * SBDMA_ADD_RCVBUFFER(d,sb)
  633. *
  634. * Add a buffer to the specified DMA channel. For receive channels,
  635. * this queues a buffer for inbound packets.
  636. *
  637. * Input parameters:
  638. * sc - softc structure
  639. * d - DMA channel descriptor
  640. * sb - sk_buff to add, or NULL if we should allocate one
  641. *
  642. * Return value:
  643. * 0 if buffer could not be added (ring is full)
  644. * 1 if buffer added successfully
  645. ********************************************************************* */
  646. static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
  647. struct sk_buff *sb)
  648. {
  649. struct net_device *dev = sc->sbm_dev;
  650. struct sbdmadscr *dsc;
  651. struct sbdmadscr *nextdsc;
  652. struct sk_buff *sb_new = NULL;
  653. int pktsize = ENET_PACKET_SIZE;
  654. /* get pointer to our current place in the ring */
  655. dsc = d->sbdma_addptr;
  656. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  657. /*
  658. * figure out if the ring is full - if the next descriptor
  659. * is the same as the one that we're going to remove from
  660. * the ring, the ring is full
  661. */
  662. if (nextdsc == d->sbdma_remptr) {
  663. return -ENOSPC;
  664. }
  665. /*
  666. * Allocate a sk_buff if we don't already have one.
  667. * If we do have an sk_buff, reset it so that it's empty.
  668. *
  669. * Note: sk_buffs don't seem to be guaranteed to have any sort
  670. * of alignment when they are allocated. Therefore, allocate enough
  671. * extra space to make sure that:
  672. *
  673. * 1. the data does not start in the middle of a cache line.
  674. * 2. The data does not end in the middle of a cache line
  675. * 3. The buffer can be aligned such that the IP addresses are
  676. * naturally aligned.
  677. *
  678. * Remember, the SOCs MAC writes whole cache lines at a time,
  679. * without reading the old contents first. So, if the sk_buff's
  680. * data portion starts in the middle of a cache line, the SOC
  681. * DMA will trash the beginning (and ending) portions.
  682. */
  683. if (sb == NULL) {
  684. sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
  685. SMP_CACHE_BYTES * 2 +
  686. NET_IP_ALIGN);
  687. if (sb_new == NULL)
  688. return -ENOBUFS;
  689. sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
  690. }
  691. else {
  692. sb_new = sb;
  693. /*
  694. * nothing special to reinit buffer, it's already aligned
  695. * and sb->data already points to a good place.
  696. */
  697. }
  698. /*
  699. * fill in the descriptor
  700. */
  701. #ifdef CONFIG_SBMAC_COALESCE
  702. /*
  703. * Do not interrupt per DMA transfer.
  704. */
  705. dsc->dscr_a = virt_to_phys(sb_new->data) |
  706. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
  707. #else
  708. dsc->dscr_a = virt_to_phys(sb_new->data) |
  709. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
  710. M_DMA_DSCRA_INTERRUPT;
  711. #endif
  712. /* receiving: no options */
  713. dsc->dscr_b = 0;
  714. /*
  715. * fill in the context
  716. */
  717. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
  718. /*
  719. * point at next packet
  720. */
  721. d->sbdma_addptr = nextdsc;
  722. /*
  723. * Give the buffer to the DMA engine.
  724. */
  725. __raw_writeq(1, d->sbdma_dscrcnt);
  726. return 0; /* we did it */
  727. }
  728. /**********************************************************************
  729. * SBDMA_ADD_TXBUFFER(d,sb)
  730. *
  731. * Add a transmit buffer to the specified DMA channel, causing a
  732. * transmit to start.
  733. *
  734. * Input parameters:
  735. * d - DMA channel descriptor
  736. * sb - sk_buff to add
  737. *
  738. * Return value:
  739. * 0 transmit queued successfully
  740. * otherwise error code
  741. ********************************************************************* */
  742. static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
  743. {
  744. struct sbdmadscr *dsc;
  745. struct sbdmadscr *nextdsc;
  746. uint64_t phys;
  747. uint64_t ncb;
  748. int length;
  749. /* get pointer to our current place in the ring */
  750. dsc = d->sbdma_addptr;
  751. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  752. /*
  753. * figure out if the ring is full - if the next descriptor
  754. * is the same as the one that we're going to remove from
  755. * the ring, the ring is full
  756. */
  757. if (nextdsc == d->sbdma_remptr) {
  758. return -ENOSPC;
  759. }
  760. /*
  761. * Under Linux, it's not necessary to copy/coalesce buffers
  762. * like it is on NetBSD. We think they're all contiguous,
  763. * but that may not be true for GBE.
  764. */
  765. length = sb->len;
  766. /*
  767. * fill in the descriptor. Note that the number of cache
  768. * blocks in the descriptor is the number of blocks
  769. * *spanned*, so we need to add in the offset (if any)
  770. * while doing the calculation.
  771. */
  772. phys = virt_to_phys(sb->data);
  773. ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
  774. dsc->dscr_a = phys |
  775. V_DMA_DSCRA_A_SIZE(ncb) |
  776. #ifndef CONFIG_SBMAC_COALESCE
  777. M_DMA_DSCRA_INTERRUPT |
  778. #endif
  779. M_DMA_ETHTX_SOP;
  780. /* transmitting: set outbound options and length */
  781. dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
  782. V_DMA_DSCRB_PKT_SIZE(length);
  783. /*
  784. * fill in the context
  785. */
  786. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
  787. /*
  788. * point at next packet
  789. */
  790. d->sbdma_addptr = nextdsc;
  791. /*
  792. * Give the buffer to the DMA engine.
  793. */
  794. __raw_writeq(1, d->sbdma_dscrcnt);
  795. return 0; /* we did it */
  796. }
  797. /**********************************************************************
  798. * SBDMA_EMPTYRING(d)
  799. *
  800. * Free all allocated sk_buffs on the specified DMA channel;
  801. *
  802. * Input parameters:
  803. * d - DMA channel
  804. *
  805. * Return value:
  806. * nothing
  807. ********************************************************************* */
  808. static void sbdma_emptyring(struct sbmacdma *d)
  809. {
  810. int idx;
  811. struct sk_buff *sb;
  812. for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
  813. sb = d->sbdma_ctxtable[idx];
  814. if (sb) {
  815. dev_kfree_skb(sb);
  816. d->sbdma_ctxtable[idx] = NULL;
  817. }
  818. }
  819. }
  820. /**********************************************************************
  821. * SBDMA_FILLRING(d)
  822. *
  823. * Fill the specified DMA channel (must be receive channel)
  824. * with sk_buffs
  825. *
  826. * Input parameters:
  827. * sc - softc structure
  828. * d - DMA channel
  829. *
  830. * Return value:
  831. * nothing
  832. ********************************************************************* */
  833. static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
  834. {
  835. int idx;
  836. for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
  837. if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
  838. break;
  839. }
  840. }
  841. #ifdef CONFIG_NET_POLL_CONTROLLER
  842. static void sbmac_netpoll(struct net_device *netdev)
  843. {
  844. struct sbmac_softc *sc = netdev_priv(netdev);
  845. int irq = sc->sbm_dev->irq;
  846. __raw_writeq(0, sc->sbm_imr);
  847. sbmac_intr(irq, netdev);
  848. #ifdef CONFIG_SBMAC_COALESCE
  849. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  850. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  851. sc->sbm_imr);
  852. #else
  853. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  854. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  855. #endif
  856. }
  857. #endif
  858. /**********************************************************************
  859. * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
  860. *
  861. * Process "completed" receive buffers on the specified DMA channel.
  862. *
  863. * Input parameters:
  864. * sc - softc structure
  865. * d - DMA channel context
  866. * work_to_do - no. of packets to process before enabling interrupt
  867. * again (for NAPI)
  868. * poll - 1: using polling (for NAPI)
  869. *
  870. * Return value:
  871. * nothing
  872. ********************************************************************* */
  873. static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  874. int work_to_do, int poll)
  875. {
  876. struct net_device *dev = sc->sbm_dev;
  877. int curidx;
  878. int hwidx;
  879. struct sbdmadscr *dsc;
  880. struct sk_buff *sb;
  881. int len;
  882. int work_done = 0;
  883. int dropped = 0;
  884. prefetch(d);
  885. again:
  886. /* Check if the HW dropped any frames */
  887. dev->stats.rx_fifo_errors
  888. += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
  889. __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
  890. while (work_to_do-- > 0) {
  891. /*
  892. * figure out where we are (as an index) and where
  893. * the hardware is (also as an index)
  894. *
  895. * This could be done faster if (for example) the
  896. * descriptor table was page-aligned and contiguous in
  897. * both virtual and physical memory -- you could then
  898. * just compare the low-order bits of the virtual address
  899. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  900. */
  901. dsc = d->sbdma_remptr;
  902. curidx = dsc - d->sbdma_dscrtable;
  903. prefetch(dsc);
  904. prefetch(&d->sbdma_ctxtable[curidx]);
  905. hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  906. d->sbdma_dscrtable_phys) /
  907. sizeof(*d->sbdma_dscrtable);
  908. /*
  909. * If they're the same, that means we've processed all
  910. * of the descriptors up to (but not including) the one that
  911. * the hardware is working on right now.
  912. */
  913. if (curidx == hwidx)
  914. goto done;
  915. /*
  916. * Otherwise, get the packet's sk_buff ptr back
  917. */
  918. sb = d->sbdma_ctxtable[curidx];
  919. d->sbdma_ctxtable[curidx] = NULL;
  920. len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
  921. /*
  922. * Check packet status. If good, process it.
  923. * If not, silently drop it and put it back on the
  924. * receive ring.
  925. */
  926. if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
  927. /*
  928. * Add a new buffer to replace the old one. If we fail
  929. * to allocate a buffer, we're going to drop this
  930. * packet and put it right back on the receive ring.
  931. */
  932. if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
  933. -ENOBUFS)) {
  934. dev->stats.rx_dropped++;
  935. /* Re-add old buffer */
  936. sbdma_add_rcvbuffer(sc, d, sb);
  937. /* No point in continuing at the moment */
  938. printk(KERN_ERR "dropped packet (1)\n");
  939. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  940. goto done;
  941. } else {
  942. /*
  943. * Set length into the packet
  944. */
  945. skb_put(sb,len);
  946. /*
  947. * Buffer has been replaced on the
  948. * receive ring. Pass the buffer to
  949. * the kernel
  950. */
  951. sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
  952. /* Check hw IPv4/TCP checksum if supported */
  953. if (sc->rx_hw_checksum == ENABLE) {
  954. if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
  955. !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
  956. sb->ip_summed = CHECKSUM_UNNECESSARY;
  957. /* don't need to set sb->csum */
  958. } else {
  959. skb_checksum_none_assert(sb);
  960. }
  961. }
  962. prefetch(sb->data);
  963. prefetch((const void *)(((char *)sb->data)+32));
  964. if (poll)
  965. dropped = netif_receive_skb(sb);
  966. else
  967. dropped = netif_rx(sb);
  968. if (dropped == NET_RX_DROP) {
  969. dev->stats.rx_dropped++;
  970. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  971. goto done;
  972. }
  973. else {
  974. dev->stats.rx_bytes += len;
  975. dev->stats.rx_packets++;
  976. }
  977. }
  978. } else {
  979. /*
  980. * Packet was mangled somehow. Just drop it and
  981. * put it back on the receive ring.
  982. */
  983. dev->stats.rx_errors++;
  984. sbdma_add_rcvbuffer(sc, d, sb);
  985. }
  986. /*
  987. * .. and advance to the next buffer.
  988. */
  989. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  990. work_done++;
  991. }
  992. if (!poll) {
  993. work_to_do = 32;
  994. goto again; /* collect fifo drop statistics again */
  995. }
  996. done:
  997. return work_done;
  998. }
  999. /**********************************************************************
  1000. * SBDMA_TX_PROCESS(sc,d)
  1001. *
  1002. * Process "completed" transmit buffers on the specified DMA channel.
  1003. * This is normally called within the interrupt service routine.
  1004. * Note that this isn't really ideal for priority channels, since
  1005. * it processes all of the packets on a given channel before
  1006. * returning.
  1007. *
  1008. * Input parameters:
  1009. * sc - softc structure
  1010. * d - DMA channel context
  1011. * poll - 1: using polling (for NAPI)
  1012. *
  1013. * Return value:
  1014. * nothing
  1015. ********************************************************************* */
  1016. static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  1017. int poll)
  1018. {
  1019. struct net_device *dev = sc->sbm_dev;
  1020. int curidx;
  1021. int hwidx;
  1022. struct sbdmadscr *dsc;
  1023. struct sk_buff *sb;
  1024. unsigned long flags;
  1025. int packets_handled = 0;
  1026. spin_lock_irqsave(&(sc->sbm_lock), flags);
  1027. if (d->sbdma_remptr == d->sbdma_addptr)
  1028. goto end_unlock;
  1029. hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  1030. d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
  1031. for (;;) {
  1032. /*
  1033. * figure out where we are (as an index) and where
  1034. * the hardware is (also as an index)
  1035. *
  1036. * This could be done faster if (for example) the
  1037. * descriptor table was page-aligned and contiguous in
  1038. * both virtual and physical memory -- you could then
  1039. * just compare the low-order bits of the virtual address
  1040. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  1041. */
  1042. curidx = d->sbdma_remptr - d->sbdma_dscrtable;
  1043. /*
  1044. * If they're the same, that means we've processed all
  1045. * of the descriptors up to (but not including) the one that
  1046. * the hardware is working on right now.
  1047. */
  1048. if (curidx == hwidx)
  1049. break;
  1050. /*
  1051. * Otherwise, get the packet's sk_buff ptr back
  1052. */
  1053. dsc = &(d->sbdma_dscrtable[curidx]);
  1054. sb = d->sbdma_ctxtable[curidx];
  1055. d->sbdma_ctxtable[curidx] = NULL;
  1056. /*
  1057. * Stats
  1058. */
  1059. dev->stats.tx_bytes += sb->len;
  1060. dev->stats.tx_packets++;
  1061. /*
  1062. * for transmits, we just free buffers.
  1063. */
  1064. dev_kfree_skb_irq(sb);
  1065. /*
  1066. * .. and advance to the next buffer.
  1067. */
  1068. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1069. packets_handled++;
  1070. }
  1071. /*
  1072. * Decide if we should wake up the protocol or not.
  1073. * Other drivers seem to do this when we reach a low
  1074. * watermark on the transmit queue.
  1075. */
  1076. if (packets_handled)
  1077. netif_wake_queue(d->sbdma_eth->sbm_dev);
  1078. end_unlock:
  1079. spin_unlock_irqrestore(&(sc->sbm_lock), flags);
  1080. }
  1081. /**********************************************************************
  1082. * SBMAC_INITCTX(s)
  1083. *
  1084. * Initialize an Ethernet context structure - this is called
  1085. * once per MAC on the 1250. Memory is allocated here, so don't
  1086. * call it again from inside the ioctl routines that bring the
  1087. * interface up/down
  1088. *
  1089. * Input parameters:
  1090. * s - sbmac context structure
  1091. *
  1092. * Return value:
  1093. * 0
  1094. ********************************************************************* */
  1095. static int sbmac_initctx(struct sbmac_softc *s)
  1096. {
  1097. /*
  1098. * figure out the addresses of some ports
  1099. */
  1100. s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
  1101. s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
  1102. s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
  1103. s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
  1104. s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
  1105. s->sbm_isr = s->sbm_base + R_MAC_STATUS;
  1106. s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
  1107. s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
  1108. /*
  1109. * Initialize the DMA channels. Right now, only one per MAC is used
  1110. * Note: Only do this _once_, as it allocates memory from the kernel!
  1111. */
  1112. sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
  1113. sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
  1114. /*
  1115. * initial state is OFF
  1116. */
  1117. s->sbm_state = sbmac_state_off;
  1118. return 0;
  1119. }
  1120. static void sbdma_uninitctx(struct sbmacdma *d)
  1121. {
  1122. kfree(d->sbdma_dscrtable_unaligned);
  1123. d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
  1124. kfree(d->sbdma_ctxtable);
  1125. d->sbdma_ctxtable = NULL;
  1126. }
  1127. static void sbmac_uninitctx(struct sbmac_softc *sc)
  1128. {
  1129. sbdma_uninitctx(&(sc->sbm_txdma));
  1130. sbdma_uninitctx(&(sc->sbm_rxdma));
  1131. }
  1132. /**********************************************************************
  1133. * SBMAC_CHANNEL_START(s)
  1134. *
  1135. * Start packet processing on this MAC.
  1136. *
  1137. * Input parameters:
  1138. * s - sbmac structure
  1139. *
  1140. * Return value:
  1141. * nothing
  1142. ********************************************************************* */
  1143. static void sbmac_channel_start(struct sbmac_softc *s)
  1144. {
  1145. uint64_t reg;
  1146. void __iomem *port;
  1147. uint64_t cfg,fifo,framecfg;
  1148. int idx, th_value;
  1149. /*
  1150. * Don't do this if running
  1151. */
  1152. if (s->sbm_state == sbmac_state_on)
  1153. return;
  1154. /*
  1155. * Bring the controller out of reset, but leave it off.
  1156. */
  1157. __raw_writeq(0, s->sbm_macenable);
  1158. /*
  1159. * Ignore all received packets
  1160. */
  1161. __raw_writeq(0, s->sbm_rxfilter);
  1162. /*
  1163. * Calculate values for various control registers.
  1164. */
  1165. cfg = M_MAC_RETRY_EN |
  1166. M_MAC_TX_HOLD_SOP_EN |
  1167. V_MAC_TX_PAUSE_CNT_16K |
  1168. M_MAC_AP_STAT_EN |
  1169. M_MAC_FAST_SYNC |
  1170. M_MAC_SS_EN |
  1171. 0;
  1172. /*
  1173. * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
  1174. * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
  1175. * Use a larger RD_THRSH for gigabit
  1176. */
  1177. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
  1178. th_value = 28;
  1179. else
  1180. th_value = 64;
  1181. fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
  1182. ((s->sbm_speed == sbmac_speed_1000)
  1183. ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
  1184. V_MAC_TX_RL_THRSH(4) |
  1185. V_MAC_RX_PL_THRSH(4) |
  1186. V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
  1187. V_MAC_RX_RL_THRSH(8) |
  1188. 0;
  1189. framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
  1190. V_MAC_MAX_FRAMESZ_DEFAULT |
  1191. V_MAC_BACKOFF_SEL(1);
  1192. /*
  1193. * Clear out the hash address map
  1194. */
  1195. port = s->sbm_base + R_MAC_HASH_BASE;
  1196. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1197. __raw_writeq(0, port);
  1198. port += sizeof(uint64_t);
  1199. }
  1200. /*
  1201. * Clear out the exact-match table
  1202. */
  1203. port = s->sbm_base + R_MAC_ADDR_BASE;
  1204. for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
  1205. __raw_writeq(0, port);
  1206. port += sizeof(uint64_t);
  1207. }
  1208. /*
  1209. * Clear out the DMA Channel mapping table registers
  1210. */
  1211. port = s->sbm_base + R_MAC_CHUP0_BASE;
  1212. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1213. __raw_writeq(0, port);
  1214. port += sizeof(uint64_t);
  1215. }
  1216. port = s->sbm_base + R_MAC_CHLO0_BASE;
  1217. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1218. __raw_writeq(0, port);
  1219. port += sizeof(uint64_t);
  1220. }
  1221. /*
  1222. * Program the hardware address. It goes into the hardware-address
  1223. * register as well as the first filter register.
  1224. */
  1225. reg = sbmac_addr2reg(s->sbm_hwaddr);
  1226. port = s->sbm_base + R_MAC_ADDR_BASE;
  1227. __raw_writeq(reg, port);
  1228. port = s->sbm_base + R_MAC_ETHERNET_ADDR;
  1229. __raw_writeq(reg, port);
  1230. /*
  1231. * Set the receive filter for no packets, and write values
  1232. * to the various config registers
  1233. */
  1234. __raw_writeq(0, s->sbm_rxfilter);
  1235. __raw_writeq(0, s->sbm_imr);
  1236. __raw_writeq(framecfg, s->sbm_framecfg);
  1237. __raw_writeq(fifo, s->sbm_fifocfg);
  1238. __raw_writeq(cfg, s->sbm_maccfg);
  1239. /*
  1240. * Initialize DMA channels (rings should be ok now)
  1241. */
  1242. sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
  1243. sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
  1244. /*
  1245. * Configure the speed, duplex, and flow control
  1246. */
  1247. sbmac_set_speed(s,s->sbm_speed);
  1248. sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
  1249. /*
  1250. * Fill the receive ring
  1251. */
  1252. sbdma_fillring(s, &(s->sbm_rxdma));
  1253. /*
  1254. * Turn on the rest of the bits in the enable register
  1255. */
  1256. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  1257. __raw_writeq(M_MAC_RXDMA_EN0 |
  1258. M_MAC_TXDMA_EN0, s->sbm_macenable);
  1259. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  1260. __raw_writeq(M_MAC_RXDMA_EN0 |
  1261. M_MAC_TXDMA_EN0 |
  1262. M_MAC_RX_ENABLE |
  1263. M_MAC_TX_ENABLE, s->sbm_macenable);
  1264. #else
  1265. #error invalid SiByte MAC configuration
  1266. #endif
  1267. #ifdef CONFIG_SBMAC_COALESCE
  1268. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  1269. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
  1270. #else
  1271. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  1272. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
  1273. #endif
  1274. /*
  1275. * Enable receiving unicasts and broadcasts
  1276. */
  1277. __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
  1278. /*
  1279. * we're running now.
  1280. */
  1281. s->sbm_state = sbmac_state_on;
  1282. /*
  1283. * Program multicast addresses
  1284. */
  1285. sbmac_setmulti(s);
  1286. /*
  1287. * If channel was in promiscuous mode before, turn that on
  1288. */
  1289. if (s->sbm_devflags & IFF_PROMISC) {
  1290. sbmac_promiscuous_mode(s,1);
  1291. }
  1292. }
  1293. /**********************************************************************
  1294. * SBMAC_CHANNEL_STOP(s)
  1295. *
  1296. * Stop packet processing on this MAC.
  1297. *
  1298. * Input parameters:
  1299. * s - sbmac structure
  1300. *
  1301. * Return value:
  1302. * nothing
  1303. ********************************************************************* */
  1304. static void sbmac_channel_stop(struct sbmac_softc *s)
  1305. {
  1306. /* don't do this if already stopped */
  1307. if (s->sbm_state == sbmac_state_off)
  1308. return;
  1309. /* don't accept any packets, disable all interrupts */
  1310. __raw_writeq(0, s->sbm_rxfilter);
  1311. __raw_writeq(0, s->sbm_imr);
  1312. /* Turn off ticker */
  1313. /* XXX */
  1314. /* turn off receiver and transmitter */
  1315. __raw_writeq(0, s->sbm_macenable);
  1316. /* We're stopped now. */
  1317. s->sbm_state = sbmac_state_off;
  1318. /*
  1319. * Stop DMA channels (rings should be ok now)
  1320. */
  1321. sbdma_channel_stop(&(s->sbm_rxdma));
  1322. sbdma_channel_stop(&(s->sbm_txdma));
  1323. /* Empty the receive and transmit rings */
  1324. sbdma_emptyring(&(s->sbm_rxdma));
  1325. sbdma_emptyring(&(s->sbm_txdma));
  1326. }
  1327. /**********************************************************************
  1328. * SBMAC_SET_CHANNEL_STATE(state)
  1329. *
  1330. * Set the channel's state ON or OFF
  1331. *
  1332. * Input parameters:
  1333. * state - new state
  1334. *
  1335. * Return value:
  1336. * old state
  1337. ********************************************************************* */
  1338. static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
  1339. enum sbmac_state state)
  1340. {
  1341. enum sbmac_state oldstate = sc->sbm_state;
  1342. /*
  1343. * If same as previous state, return
  1344. */
  1345. if (state == oldstate) {
  1346. return oldstate;
  1347. }
  1348. /*
  1349. * If new state is ON, turn channel on
  1350. */
  1351. if (state == sbmac_state_on) {
  1352. sbmac_channel_start(sc);
  1353. }
  1354. else {
  1355. sbmac_channel_stop(sc);
  1356. }
  1357. /*
  1358. * Return previous state
  1359. */
  1360. return oldstate;
  1361. }
  1362. /**********************************************************************
  1363. * SBMAC_PROMISCUOUS_MODE(sc,onoff)
  1364. *
  1365. * Turn on or off promiscuous mode
  1366. *
  1367. * Input parameters:
  1368. * sc - softc
  1369. * onoff - 1 to turn on, 0 to turn off
  1370. *
  1371. * Return value:
  1372. * nothing
  1373. ********************************************************************* */
  1374. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
  1375. {
  1376. uint64_t reg;
  1377. if (sc->sbm_state != sbmac_state_on)
  1378. return;
  1379. if (onoff) {
  1380. reg = __raw_readq(sc->sbm_rxfilter);
  1381. reg |= M_MAC_ALLPKT_EN;
  1382. __raw_writeq(reg, sc->sbm_rxfilter);
  1383. }
  1384. else {
  1385. reg = __raw_readq(sc->sbm_rxfilter);
  1386. reg &= ~M_MAC_ALLPKT_EN;
  1387. __raw_writeq(reg, sc->sbm_rxfilter);
  1388. }
  1389. }
  1390. /**********************************************************************
  1391. * SBMAC_SETIPHDR_OFFSET(sc,onoff)
  1392. *
  1393. * Set the iphdr offset as 15 assuming ethernet encapsulation
  1394. *
  1395. * Input parameters:
  1396. * sc - softc
  1397. *
  1398. * Return value:
  1399. * nothing
  1400. ********************************************************************* */
  1401. static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
  1402. {
  1403. uint64_t reg;
  1404. /* Hard code the off set to 15 for now */
  1405. reg = __raw_readq(sc->sbm_rxfilter);
  1406. reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
  1407. __raw_writeq(reg, sc->sbm_rxfilter);
  1408. /* BCM1250 pass1 didn't have hardware checksum. Everything
  1409. later does. */
  1410. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
  1411. sc->rx_hw_checksum = DISABLE;
  1412. } else {
  1413. sc->rx_hw_checksum = ENABLE;
  1414. }
  1415. }
  1416. /**********************************************************************
  1417. * SBMAC_ADDR2REG(ptr)
  1418. *
  1419. * Convert six bytes into the 64-bit register value that
  1420. * we typically write into the SBMAC's address/mcast registers
  1421. *
  1422. * Input parameters:
  1423. * ptr - pointer to 6 bytes
  1424. *
  1425. * Return value:
  1426. * register value
  1427. ********************************************************************* */
  1428. static uint64_t sbmac_addr2reg(unsigned char *ptr)
  1429. {
  1430. uint64_t reg = 0;
  1431. ptr += 6;
  1432. reg |= (uint64_t) *(--ptr);
  1433. reg <<= 8;
  1434. reg |= (uint64_t) *(--ptr);
  1435. reg <<= 8;
  1436. reg |= (uint64_t) *(--ptr);
  1437. reg <<= 8;
  1438. reg |= (uint64_t) *(--ptr);
  1439. reg <<= 8;
  1440. reg |= (uint64_t) *(--ptr);
  1441. reg <<= 8;
  1442. reg |= (uint64_t) *(--ptr);
  1443. return reg;
  1444. }
  1445. /**********************************************************************
  1446. * SBMAC_SET_SPEED(s,speed)
  1447. *
  1448. * Configure LAN speed for the specified MAC.
  1449. * Warning: must be called when MAC is off!
  1450. *
  1451. * Input parameters:
  1452. * s - sbmac structure
  1453. * speed - speed to set MAC to (see enum sbmac_speed)
  1454. *
  1455. * Return value:
  1456. * 1 if successful
  1457. * 0 indicates invalid parameters
  1458. ********************************************************************* */
  1459. static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
  1460. {
  1461. uint64_t cfg;
  1462. uint64_t framecfg;
  1463. /*
  1464. * Save new current values
  1465. */
  1466. s->sbm_speed = speed;
  1467. if (s->sbm_state == sbmac_state_on)
  1468. return 0; /* save for next restart */
  1469. /*
  1470. * Read current register values
  1471. */
  1472. cfg = __raw_readq(s->sbm_maccfg);
  1473. framecfg = __raw_readq(s->sbm_framecfg);
  1474. /*
  1475. * Mask out the stuff we want to change
  1476. */
  1477. cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
  1478. framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
  1479. M_MAC_SLOT_SIZE);
  1480. /*
  1481. * Now add in the new bits
  1482. */
  1483. switch (speed) {
  1484. case sbmac_speed_10:
  1485. framecfg |= V_MAC_IFG_RX_10 |
  1486. V_MAC_IFG_TX_10 |
  1487. K_MAC_IFG_THRSH_10 |
  1488. V_MAC_SLOT_SIZE_10;
  1489. cfg |= V_MAC_SPEED_SEL_10MBPS;
  1490. break;
  1491. case sbmac_speed_100:
  1492. framecfg |= V_MAC_IFG_RX_100 |
  1493. V_MAC_IFG_TX_100 |
  1494. V_MAC_IFG_THRSH_100 |
  1495. V_MAC_SLOT_SIZE_100;
  1496. cfg |= V_MAC_SPEED_SEL_100MBPS ;
  1497. break;
  1498. case sbmac_speed_1000:
  1499. framecfg |= V_MAC_IFG_RX_1000 |
  1500. V_MAC_IFG_TX_1000 |
  1501. V_MAC_IFG_THRSH_1000 |
  1502. V_MAC_SLOT_SIZE_1000;
  1503. cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
  1504. break;
  1505. default:
  1506. return 0;
  1507. }
  1508. /*
  1509. * Send the bits back to the hardware
  1510. */
  1511. __raw_writeq(framecfg, s->sbm_framecfg);
  1512. __raw_writeq(cfg, s->sbm_maccfg);
  1513. return 1;
  1514. }
  1515. /**********************************************************************
  1516. * SBMAC_SET_DUPLEX(s,duplex,fc)
  1517. *
  1518. * Set Ethernet duplex and flow control options for this MAC
  1519. * Warning: must be called when MAC is off!
  1520. *
  1521. * Input parameters:
  1522. * s - sbmac structure
  1523. * duplex - duplex setting (see enum sbmac_duplex)
  1524. * fc - flow control setting (see enum sbmac_fc)
  1525. *
  1526. * Return value:
  1527. * 1 if ok
  1528. * 0 if an invalid parameter combination was specified
  1529. ********************************************************************* */
  1530. static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
  1531. enum sbmac_fc fc)
  1532. {
  1533. uint64_t cfg;
  1534. /*
  1535. * Save new current values
  1536. */
  1537. s->sbm_duplex = duplex;
  1538. s->sbm_fc = fc;
  1539. if (s->sbm_state == sbmac_state_on)
  1540. return 0; /* save for next restart */
  1541. /*
  1542. * Read current register values
  1543. */
  1544. cfg = __raw_readq(s->sbm_maccfg);
  1545. /*
  1546. * Mask off the stuff we're about to change
  1547. */
  1548. cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
  1549. switch (duplex) {
  1550. case sbmac_duplex_half:
  1551. switch (fc) {
  1552. case sbmac_fc_disabled:
  1553. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
  1554. break;
  1555. case sbmac_fc_collision:
  1556. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
  1557. break;
  1558. case sbmac_fc_carrier:
  1559. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
  1560. break;
  1561. case sbmac_fc_frame: /* not valid in half duplex */
  1562. default: /* invalid selection */
  1563. return 0;
  1564. }
  1565. break;
  1566. case sbmac_duplex_full:
  1567. switch (fc) {
  1568. case sbmac_fc_disabled:
  1569. cfg |= V_MAC_FC_CMD_DISABLED;
  1570. break;
  1571. case sbmac_fc_frame:
  1572. cfg |= V_MAC_FC_CMD_ENABLED;
  1573. break;
  1574. case sbmac_fc_collision: /* not valid in full duplex */
  1575. case sbmac_fc_carrier: /* not valid in full duplex */
  1576. default:
  1577. return 0;
  1578. }
  1579. break;
  1580. default:
  1581. return 0;
  1582. }
  1583. /*
  1584. * Send the bits back to the hardware
  1585. */
  1586. __raw_writeq(cfg, s->sbm_maccfg);
  1587. return 1;
  1588. }
  1589. /**********************************************************************
  1590. * SBMAC_INTR()
  1591. *
  1592. * Interrupt handler for MAC interrupts
  1593. *
  1594. * Input parameters:
  1595. * MAC structure
  1596. *
  1597. * Return value:
  1598. * nothing
  1599. ********************************************************************* */
  1600. static irqreturn_t sbmac_intr(int irq,void *dev_instance)
  1601. {
  1602. struct net_device *dev = (struct net_device *) dev_instance;
  1603. struct sbmac_softc *sc = netdev_priv(dev);
  1604. uint64_t isr;
  1605. int handled = 0;
  1606. /*
  1607. * Read the ISR (this clears the bits in the real
  1608. * register, except for counter addr)
  1609. */
  1610. isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
  1611. if (isr == 0)
  1612. return IRQ_RETVAL(0);
  1613. handled = 1;
  1614. /*
  1615. * Transmits on channel 0
  1616. */
  1617. if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
  1618. sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
  1619. if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
  1620. if (napi_schedule_prep(&sc->napi)) {
  1621. __raw_writeq(0, sc->sbm_imr);
  1622. __napi_schedule(&sc->napi);
  1623. /* Depend on the exit from poll to reenable intr */
  1624. }
  1625. else {
  1626. /* may leave some packets behind */
  1627. sbdma_rx_process(sc,&(sc->sbm_rxdma),
  1628. SBMAC_MAX_RXDESCR * 2, 0);
  1629. }
  1630. }
  1631. return IRQ_RETVAL(handled);
  1632. }
  1633. /**********************************************************************
  1634. * SBMAC_START_TX(skb,dev)
  1635. *
  1636. * Start output on the specified interface. Basically, we
  1637. * queue as many buffers as we can until the ring fills up, or
  1638. * we run off the end of the queue, whichever comes first.
  1639. *
  1640. * Input parameters:
  1641. *
  1642. *
  1643. * Return value:
  1644. * nothing
  1645. ********************************************************************* */
  1646. static netdev_tx_t sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
  1647. {
  1648. struct sbmac_softc *sc = netdev_priv(dev);
  1649. unsigned long flags;
  1650. /* lock eth irq */
  1651. spin_lock_irqsave(&sc->sbm_lock, flags);
  1652. /*
  1653. * Put the buffer on the transmit ring. If we
  1654. * don't have room, stop the queue.
  1655. */
  1656. if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
  1657. /* XXX save skb that we could not send */
  1658. netif_stop_queue(dev);
  1659. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1660. return NETDEV_TX_BUSY;
  1661. }
  1662. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1663. return NETDEV_TX_OK;
  1664. }
  1665. /**********************************************************************
  1666. * SBMAC_SETMULTI(sc)
  1667. *
  1668. * Reprogram the multicast table into the hardware, given
  1669. * the list of multicasts associated with the interface
  1670. * structure.
  1671. *
  1672. * Input parameters:
  1673. * sc - softc
  1674. *
  1675. * Return value:
  1676. * nothing
  1677. ********************************************************************* */
  1678. static void sbmac_setmulti(struct sbmac_softc *sc)
  1679. {
  1680. uint64_t reg;
  1681. void __iomem *port;
  1682. int idx;
  1683. struct netdev_hw_addr *ha;
  1684. struct net_device *dev = sc->sbm_dev;
  1685. /*
  1686. * Clear out entire multicast table. We do this by nuking
  1687. * the entire hash table and all the direct matches except
  1688. * the first one, which is used for our station address
  1689. */
  1690. for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
  1691. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
  1692. __raw_writeq(0, port);
  1693. }
  1694. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1695. port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
  1696. __raw_writeq(0, port);
  1697. }
  1698. /*
  1699. * Clear the filter to say we don't want any multicasts.
  1700. */
  1701. reg = __raw_readq(sc->sbm_rxfilter);
  1702. reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1703. __raw_writeq(reg, sc->sbm_rxfilter);
  1704. if (dev->flags & IFF_ALLMULTI) {
  1705. /*
  1706. * Enable ALL multicasts. Do this by inverting the
  1707. * multicast enable bit.
  1708. */
  1709. reg = __raw_readq(sc->sbm_rxfilter);
  1710. reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1711. __raw_writeq(reg, sc->sbm_rxfilter);
  1712. return;
  1713. }
  1714. /*
  1715. * Progam new multicast entries. For now, only use the
  1716. * perfect filter. In the future we'll need to use the
  1717. * hash filter if the perfect filter overflows
  1718. */
  1719. /* XXX only using perfect filter for now, need to use hash
  1720. * XXX if the table overflows */
  1721. idx = 1; /* skip station address */
  1722. netdev_for_each_mc_addr(ha, dev) {
  1723. if (idx == MAC_ADDR_COUNT)
  1724. break;
  1725. reg = sbmac_addr2reg(ha->addr);
  1726. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
  1727. __raw_writeq(reg, port);
  1728. idx++;
  1729. }
  1730. /*
  1731. * Enable the "accept multicast bits" if we programmed at least one
  1732. * multicast.
  1733. */
  1734. if (idx > 1) {
  1735. reg = __raw_readq(sc->sbm_rxfilter);
  1736. reg |= M_MAC_MCAST_EN;
  1737. __raw_writeq(reg, sc->sbm_rxfilter);
  1738. }
  1739. }
  1740. static const struct net_device_ops sbmac_netdev_ops = {
  1741. .ndo_open = sbmac_open,
  1742. .ndo_stop = sbmac_close,
  1743. .ndo_start_xmit = sbmac_start_tx,
  1744. .ndo_set_rx_mode = sbmac_set_rx_mode,
  1745. .ndo_tx_timeout = sbmac_tx_timeout,
  1746. .ndo_do_ioctl = sbmac_mii_ioctl,
  1747. .ndo_validate_addr = eth_validate_addr,
  1748. .ndo_set_mac_address = eth_mac_addr,
  1749. #ifdef CONFIG_NET_POLL_CONTROLLER
  1750. .ndo_poll_controller = sbmac_netpoll,
  1751. #endif
  1752. };
  1753. /**********************************************************************
  1754. * SBMAC_INIT(dev)
  1755. *
  1756. * Attach routine - init hardware and hook ourselves into linux
  1757. *
  1758. * Input parameters:
  1759. * dev - net_device structure
  1760. *
  1761. * Return value:
  1762. * status
  1763. ********************************************************************* */
  1764. static int sbmac_init(struct platform_device *pldev, long long base)
  1765. {
  1766. struct net_device *dev = platform_get_drvdata(pldev);
  1767. int idx = pldev->id;
  1768. struct sbmac_softc *sc = netdev_priv(dev);
  1769. unsigned char *eaddr;
  1770. uint64_t ea_reg;
  1771. int i;
  1772. int err;
  1773. sc->sbm_dev = dev;
  1774. sc->sbe_idx = idx;
  1775. eaddr = sc->sbm_hwaddr;
  1776. /*
  1777. * Read the ethernet address. The firmware left this programmed
  1778. * for us in the ethernet address register for each mac.
  1779. */
  1780. ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1781. __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1782. for (i = 0; i < 6; i++) {
  1783. eaddr[i] = (uint8_t) (ea_reg & 0xFF);
  1784. ea_reg >>= 8;
  1785. }
  1786. for (i = 0; i < 6; i++) {
  1787. dev->dev_addr[i] = eaddr[i];
  1788. }
  1789. /*
  1790. * Initialize context (get pointers to registers and stuff), then
  1791. * allocate the memory for the descriptor tables.
  1792. */
  1793. sbmac_initctx(sc);
  1794. /*
  1795. * Set up Linux device callins
  1796. */
  1797. spin_lock_init(&(sc->sbm_lock));
  1798. dev->netdev_ops = &sbmac_netdev_ops;
  1799. dev->watchdog_timeo = TX_TIMEOUT;
  1800. dev->min_mtu = 0;
  1801. dev->max_mtu = ENET_PACKET_SIZE;
  1802. netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
  1803. dev->irq = UNIT_INT(idx);
  1804. /* This is needed for PASS2 for Rx H/W checksum feature */
  1805. sbmac_set_iphdr_offset(sc);
  1806. sc->mii_bus = mdiobus_alloc();
  1807. if (sc->mii_bus == NULL) {
  1808. err = -ENOMEM;
  1809. goto uninit_ctx;
  1810. }
  1811. sc->mii_bus->name = sbmac_mdio_string;
  1812. snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1813. pldev->name, idx);
  1814. sc->mii_bus->priv = sc;
  1815. sc->mii_bus->read = sbmac_mii_read;
  1816. sc->mii_bus->write = sbmac_mii_write;
  1817. sc->mii_bus->parent = &pldev->dev;
  1818. /*
  1819. * Probe PHY address
  1820. */
  1821. err = mdiobus_register(sc->mii_bus);
  1822. if (err) {
  1823. printk(KERN_ERR "%s: unable to register MDIO bus\n",
  1824. dev->name);
  1825. goto free_mdio;
  1826. }
  1827. platform_set_drvdata(pldev, sc->mii_bus);
  1828. err = register_netdev(dev);
  1829. if (err) {
  1830. printk(KERN_ERR "%s.%d: unable to register netdev\n",
  1831. sbmac_string, idx);
  1832. goto unreg_mdio;
  1833. }
  1834. pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
  1835. if (sc->rx_hw_checksum == ENABLE)
  1836. pr_info("%s: enabling TCP rcv checksum\n", dev->name);
  1837. /*
  1838. * Display Ethernet address (this is called during the config
  1839. * process so we need to finish off the config message that
  1840. * was being displayed)
  1841. */
  1842. pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
  1843. dev->name, base, eaddr);
  1844. return 0;
  1845. unreg_mdio:
  1846. mdiobus_unregister(sc->mii_bus);
  1847. free_mdio:
  1848. mdiobus_free(sc->mii_bus);
  1849. uninit_ctx:
  1850. sbmac_uninitctx(sc);
  1851. return err;
  1852. }
  1853. static int sbmac_open(struct net_device *dev)
  1854. {
  1855. struct sbmac_softc *sc = netdev_priv(dev);
  1856. int err;
  1857. if (debug > 1)
  1858. pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
  1859. /*
  1860. * map/route interrupt (clear status first, in case something
  1861. * weird is pending; we haven't initialized the mac registers
  1862. * yet)
  1863. */
  1864. __raw_readq(sc->sbm_isr);
  1865. err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
  1866. if (err) {
  1867. printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
  1868. dev->irq);
  1869. goto out_err;
  1870. }
  1871. sc->sbm_speed = sbmac_speed_none;
  1872. sc->sbm_duplex = sbmac_duplex_none;
  1873. sc->sbm_fc = sbmac_fc_none;
  1874. sc->sbm_pause = -1;
  1875. sc->sbm_link = 0;
  1876. /*
  1877. * Attach to the PHY
  1878. */
  1879. err = sbmac_mii_probe(dev);
  1880. if (err)
  1881. goto out_unregister;
  1882. /*
  1883. * Turn on the channel
  1884. */
  1885. sbmac_set_channel_state(sc,sbmac_state_on);
  1886. netif_start_queue(dev);
  1887. sbmac_set_rx_mode(dev);
  1888. phy_start(sc->phy_dev);
  1889. napi_enable(&sc->napi);
  1890. return 0;
  1891. out_unregister:
  1892. free_irq(dev->irq, dev);
  1893. out_err:
  1894. return err;
  1895. }
  1896. static int sbmac_mii_probe(struct net_device *dev)
  1897. {
  1898. struct sbmac_softc *sc = netdev_priv(dev);
  1899. struct phy_device *phy_dev;
  1900. phy_dev = phy_find_first(sc->mii_bus);
  1901. if (!phy_dev) {
  1902. printk(KERN_ERR "%s: no PHY found\n", dev->name);
  1903. return -ENXIO;
  1904. }
  1905. phy_dev = phy_connect(dev, dev_name(&phy_dev->mdio.dev),
  1906. &sbmac_mii_poll, PHY_INTERFACE_MODE_GMII);
  1907. if (IS_ERR(phy_dev)) {
  1908. printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
  1909. return PTR_ERR(phy_dev);
  1910. }
  1911. /* Remove any features not supported by the controller */
  1912. phy_dev->supported &= SUPPORTED_10baseT_Half |
  1913. SUPPORTED_10baseT_Full |
  1914. SUPPORTED_100baseT_Half |
  1915. SUPPORTED_100baseT_Full |
  1916. SUPPORTED_1000baseT_Half |
  1917. SUPPORTED_1000baseT_Full |
  1918. SUPPORTED_Autoneg |
  1919. SUPPORTED_MII |
  1920. SUPPORTED_Pause |
  1921. SUPPORTED_Asym_Pause;
  1922. phy_attached_info(phy_dev);
  1923. phy_dev->advertising = phy_dev->supported;
  1924. sc->phy_dev = phy_dev;
  1925. return 0;
  1926. }
  1927. static void sbmac_mii_poll(struct net_device *dev)
  1928. {
  1929. struct sbmac_softc *sc = netdev_priv(dev);
  1930. struct phy_device *phy_dev = sc->phy_dev;
  1931. unsigned long flags;
  1932. enum sbmac_fc fc;
  1933. int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
  1934. link_chg = (sc->sbm_link != phy_dev->link);
  1935. speed_chg = (sc->sbm_speed != phy_dev->speed);
  1936. duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
  1937. pause_chg = (sc->sbm_pause != phy_dev->pause);
  1938. if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
  1939. return; /* Hmmm... */
  1940. if (!phy_dev->link) {
  1941. if (link_chg) {
  1942. sc->sbm_link = phy_dev->link;
  1943. sc->sbm_speed = sbmac_speed_none;
  1944. sc->sbm_duplex = sbmac_duplex_none;
  1945. sc->sbm_fc = sbmac_fc_disabled;
  1946. sc->sbm_pause = -1;
  1947. pr_info("%s: link unavailable\n", dev->name);
  1948. }
  1949. return;
  1950. }
  1951. if (phy_dev->duplex == DUPLEX_FULL) {
  1952. if (phy_dev->pause)
  1953. fc = sbmac_fc_frame;
  1954. else
  1955. fc = sbmac_fc_disabled;
  1956. } else
  1957. fc = sbmac_fc_collision;
  1958. fc_chg = (sc->sbm_fc != fc);
  1959. pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
  1960. phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
  1961. spin_lock_irqsave(&sc->sbm_lock, flags);
  1962. sc->sbm_speed = phy_dev->speed;
  1963. sc->sbm_duplex = phy_dev->duplex;
  1964. sc->sbm_fc = fc;
  1965. sc->sbm_pause = phy_dev->pause;
  1966. sc->sbm_link = phy_dev->link;
  1967. if ((speed_chg || duplex_chg || fc_chg) &&
  1968. sc->sbm_state != sbmac_state_off) {
  1969. /*
  1970. * something changed, restart the channel
  1971. */
  1972. if (debug > 1)
  1973. pr_debug("%s: restarting channel "
  1974. "because PHY state changed\n", dev->name);
  1975. sbmac_channel_stop(sc);
  1976. sbmac_channel_start(sc);
  1977. }
  1978. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1979. }
  1980. static void sbmac_tx_timeout (struct net_device *dev)
  1981. {
  1982. struct sbmac_softc *sc = netdev_priv(dev);
  1983. unsigned long flags;
  1984. spin_lock_irqsave(&sc->sbm_lock, flags);
  1985. netif_trans_update(dev); /* prevent tx timeout */
  1986. dev->stats.tx_errors++;
  1987. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1988. printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
  1989. }
  1990. static void sbmac_set_rx_mode(struct net_device *dev)
  1991. {
  1992. unsigned long flags;
  1993. struct sbmac_softc *sc = netdev_priv(dev);
  1994. spin_lock_irqsave(&sc->sbm_lock, flags);
  1995. if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
  1996. /*
  1997. * Promiscuous changed.
  1998. */
  1999. if (dev->flags & IFF_PROMISC) {
  2000. sbmac_promiscuous_mode(sc,1);
  2001. }
  2002. else {
  2003. sbmac_promiscuous_mode(sc,0);
  2004. }
  2005. }
  2006. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2007. /*
  2008. * Program the multicasts. Do this every time.
  2009. */
  2010. sbmac_setmulti(sc);
  2011. }
  2012. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2013. {
  2014. struct sbmac_softc *sc = netdev_priv(dev);
  2015. if (!netif_running(dev) || !sc->phy_dev)
  2016. return -EINVAL;
  2017. return phy_mii_ioctl(sc->phy_dev, rq, cmd);
  2018. }
  2019. static int sbmac_close(struct net_device *dev)
  2020. {
  2021. struct sbmac_softc *sc = netdev_priv(dev);
  2022. napi_disable(&sc->napi);
  2023. phy_stop(sc->phy_dev);
  2024. sbmac_set_channel_state(sc, sbmac_state_off);
  2025. netif_stop_queue(dev);
  2026. if (debug > 1)
  2027. pr_debug("%s: Shutting down ethercard\n", dev->name);
  2028. phy_disconnect(sc->phy_dev);
  2029. sc->phy_dev = NULL;
  2030. free_irq(dev->irq, dev);
  2031. sbdma_emptyring(&(sc->sbm_txdma));
  2032. sbdma_emptyring(&(sc->sbm_rxdma));
  2033. return 0;
  2034. }
  2035. static int sbmac_poll(struct napi_struct *napi, int budget)
  2036. {
  2037. struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
  2038. int work_done;
  2039. work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
  2040. sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
  2041. if (work_done < budget) {
  2042. napi_complete_done(napi, work_done);
  2043. #ifdef CONFIG_SBMAC_COALESCE
  2044. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  2045. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  2046. sc->sbm_imr);
  2047. #else
  2048. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  2049. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  2050. #endif
  2051. }
  2052. return work_done;
  2053. }
  2054. static int sbmac_probe(struct platform_device *pldev)
  2055. {
  2056. struct net_device *dev;
  2057. struct sbmac_softc *sc;
  2058. void __iomem *sbm_base;
  2059. struct resource *res;
  2060. u64 sbmac_orig_hwaddr;
  2061. int err;
  2062. res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
  2063. BUG_ON(!res);
  2064. sbm_base = ioremap_nocache(res->start, resource_size(res));
  2065. if (!sbm_base) {
  2066. printk(KERN_ERR "%s: unable to map device registers\n",
  2067. dev_name(&pldev->dev));
  2068. err = -ENOMEM;
  2069. goto out_out;
  2070. }
  2071. /*
  2072. * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
  2073. * value for us by the firmware if we're going to use this MAC.
  2074. * If we find a zero, skip this MAC.
  2075. */
  2076. sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
  2077. pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
  2078. sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
  2079. if (sbmac_orig_hwaddr == 0) {
  2080. err = 0;
  2081. goto out_unmap;
  2082. }
  2083. /*
  2084. * Okay, cool. Initialize this MAC.
  2085. */
  2086. dev = alloc_etherdev(sizeof(struct sbmac_softc));
  2087. if (!dev) {
  2088. err = -ENOMEM;
  2089. goto out_unmap;
  2090. }
  2091. platform_set_drvdata(pldev, dev);
  2092. SET_NETDEV_DEV(dev, &pldev->dev);
  2093. sc = netdev_priv(dev);
  2094. sc->sbm_base = sbm_base;
  2095. err = sbmac_init(pldev, res->start);
  2096. if (err)
  2097. goto out_kfree;
  2098. return 0;
  2099. out_kfree:
  2100. free_netdev(dev);
  2101. __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
  2102. out_unmap:
  2103. iounmap(sbm_base);
  2104. out_out:
  2105. return err;
  2106. }
  2107. static int sbmac_remove(struct platform_device *pldev)
  2108. {
  2109. struct net_device *dev = platform_get_drvdata(pldev);
  2110. struct sbmac_softc *sc = netdev_priv(dev);
  2111. unregister_netdev(dev);
  2112. sbmac_uninitctx(sc);
  2113. mdiobus_unregister(sc->mii_bus);
  2114. mdiobus_free(sc->mii_bus);
  2115. iounmap(sc->sbm_base);
  2116. free_netdev(dev);
  2117. return 0;
  2118. }
  2119. static struct platform_driver sbmac_driver = {
  2120. .probe = sbmac_probe,
  2121. .remove = sbmac_remove,
  2122. .driver = {
  2123. .name = sbmac_string,
  2124. },
  2125. };
  2126. module_platform_driver(sbmac_driver);
  2127. MODULE_LICENSE("GPL");