sb1250-mac.c 63 KB

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