bcm63xx_enet.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938
  1. /*
  2. * Driver for BCM963xx builtin Ethernet mac
  3. *
  4. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/clk.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/ethtool.h>
  28. #include <linux/crc32.h>
  29. #include <linux/err.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/if_vlan.h>
  33. #include <bcm63xx_dev_enet.h>
  34. #include "bcm63xx_enet.h"
  35. static char bcm_enet_driver_name[] = "bcm63xx_enet";
  36. static char bcm_enet_driver_version[] = "1.0";
  37. static int copybreak __read_mostly = 128;
  38. module_param(copybreak, int, 0);
  39. MODULE_PARM_DESC(copybreak, "Receive copy threshold");
  40. /* io registers memory shared between all devices */
  41. static void __iomem *bcm_enet_shared_base[3];
  42. /*
  43. * io helpers to access mac registers
  44. */
  45. static inline u32 enet_readl(struct bcm_enet_priv *priv, u32 off)
  46. {
  47. return bcm_readl(priv->base + off);
  48. }
  49. static inline void enet_writel(struct bcm_enet_priv *priv,
  50. u32 val, u32 off)
  51. {
  52. bcm_writel(val, priv->base + off);
  53. }
  54. /*
  55. * io helpers to access switch registers
  56. */
  57. static inline u32 enetsw_readl(struct bcm_enet_priv *priv, u32 off)
  58. {
  59. return bcm_readl(priv->base + off);
  60. }
  61. static inline void enetsw_writel(struct bcm_enet_priv *priv,
  62. u32 val, u32 off)
  63. {
  64. bcm_writel(val, priv->base + off);
  65. }
  66. static inline u16 enetsw_readw(struct bcm_enet_priv *priv, u32 off)
  67. {
  68. return bcm_readw(priv->base + off);
  69. }
  70. static inline void enetsw_writew(struct bcm_enet_priv *priv,
  71. u16 val, u32 off)
  72. {
  73. bcm_writew(val, priv->base + off);
  74. }
  75. static inline u8 enetsw_readb(struct bcm_enet_priv *priv, u32 off)
  76. {
  77. return bcm_readb(priv->base + off);
  78. }
  79. static inline void enetsw_writeb(struct bcm_enet_priv *priv,
  80. u8 val, u32 off)
  81. {
  82. bcm_writeb(val, priv->base + off);
  83. }
  84. /* io helpers to access shared registers */
  85. static inline u32 enet_dma_readl(struct bcm_enet_priv *priv, u32 off)
  86. {
  87. return bcm_readl(bcm_enet_shared_base[0] + off);
  88. }
  89. static inline void enet_dma_writel(struct bcm_enet_priv *priv,
  90. u32 val, u32 off)
  91. {
  92. bcm_writel(val, bcm_enet_shared_base[0] + off);
  93. }
  94. static inline u32 enet_dmac_readl(struct bcm_enet_priv *priv, u32 off, int chan)
  95. {
  96. return bcm_readl(bcm_enet_shared_base[1] +
  97. bcm63xx_enetdmacreg(off) + chan * priv->dma_chan_width);
  98. }
  99. static inline void enet_dmac_writel(struct bcm_enet_priv *priv,
  100. u32 val, u32 off, int chan)
  101. {
  102. bcm_writel(val, bcm_enet_shared_base[1] +
  103. bcm63xx_enetdmacreg(off) + chan * priv->dma_chan_width);
  104. }
  105. static inline u32 enet_dmas_readl(struct bcm_enet_priv *priv, u32 off, int chan)
  106. {
  107. return bcm_readl(bcm_enet_shared_base[2] + off + chan * priv->dma_chan_width);
  108. }
  109. static inline void enet_dmas_writel(struct bcm_enet_priv *priv,
  110. u32 val, u32 off, int chan)
  111. {
  112. bcm_writel(val, bcm_enet_shared_base[2] + off + chan * priv->dma_chan_width);
  113. }
  114. /*
  115. * write given data into mii register and wait for transfer to end
  116. * with timeout (average measured transfer time is 25us)
  117. */
  118. static int do_mdio_op(struct bcm_enet_priv *priv, unsigned int data)
  119. {
  120. int limit;
  121. /* make sure mii interrupt status is cleared */
  122. enet_writel(priv, ENET_IR_MII, ENET_IR_REG);
  123. enet_writel(priv, data, ENET_MIIDATA_REG);
  124. wmb();
  125. /* busy wait on mii interrupt bit, with timeout */
  126. limit = 1000;
  127. do {
  128. if (enet_readl(priv, ENET_IR_REG) & ENET_IR_MII)
  129. break;
  130. udelay(1);
  131. } while (limit-- > 0);
  132. return (limit < 0) ? 1 : 0;
  133. }
  134. /*
  135. * MII internal read callback
  136. */
  137. static int bcm_enet_mdio_read(struct bcm_enet_priv *priv, int mii_id,
  138. int regnum)
  139. {
  140. u32 tmp, val;
  141. tmp = regnum << ENET_MIIDATA_REG_SHIFT;
  142. tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
  143. tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
  144. tmp |= ENET_MIIDATA_OP_READ_MASK;
  145. if (do_mdio_op(priv, tmp))
  146. return -1;
  147. val = enet_readl(priv, ENET_MIIDATA_REG);
  148. val &= 0xffff;
  149. return val;
  150. }
  151. /*
  152. * MII internal write callback
  153. */
  154. static int bcm_enet_mdio_write(struct bcm_enet_priv *priv, int mii_id,
  155. int regnum, u16 value)
  156. {
  157. u32 tmp;
  158. tmp = (value & 0xffff) << ENET_MIIDATA_DATA_SHIFT;
  159. tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
  160. tmp |= regnum << ENET_MIIDATA_REG_SHIFT;
  161. tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
  162. tmp |= ENET_MIIDATA_OP_WRITE_MASK;
  163. (void)do_mdio_op(priv, tmp);
  164. return 0;
  165. }
  166. /*
  167. * MII read callback from phylib
  168. */
  169. static int bcm_enet_mdio_read_phylib(struct mii_bus *bus, int mii_id,
  170. int regnum)
  171. {
  172. return bcm_enet_mdio_read(bus->priv, mii_id, regnum);
  173. }
  174. /*
  175. * MII write callback from phylib
  176. */
  177. static int bcm_enet_mdio_write_phylib(struct mii_bus *bus, int mii_id,
  178. int regnum, u16 value)
  179. {
  180. return bcm_enet_mdio_write(bus->priv, mii_id, regnum, value);
  181. }
  182. /*
  183. * MII read callback from mii core
  184. */
  185. static int bcm_enet_mdio_read_mii(struct net_device *dev, int mii_id,
  186. int regnum)
  187. {
  188. return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
  189. }
  190. /*
  191. * MII write callback from mii core
  192. */
  193. static void bcm_enet_mdio_write_mii(struct net_device *dev, int mii_id,
  194. int regnum, int value)
  195. {
  196. bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
  197. }
  198. /*
  199. * refill rx queue
  200. */
  201. static int bcm_enet_refill_rx(struct net_device *dev)
  202. {
  203. struct bcm_enet_priv *priv;
  204. priv = netdev_priv(dev);
  205. while (priv->rx_desc_count < priv->rx_ring_size) {
  206. struct bcm_enet_desc *desc;
  207. struct sk_buff *skb;
  208. dma_addr_t p;
  209. int desc_idx;
  210. u32 len_stat;
  211. desc_idx = priv->rx_dirty_desc;
  212. desc = &priv->rx_desc_cpu[desc_idx];
  213. if (!priv->rx_skb[desc_idx]) {
  214. skb = netdev_alloc_skb(dev, priv->rx_skb_size);
  215. if (!skb)
  216. break;
  217. priv->rx_skb[desc_idx] = skb;
  218. p = dma_map_single(&priv->pdev->dev, skb->data,
  219. priv->rx_skb_size,
  220. DMA_FROM_DEVICE);
  221. desc->address = p;
  222. }
  223. len_stat = priv->rx_skb_size << DMADESC_LENGTH_SHIFT;
  224. len_stat |= DMADESC_OWNER_MASK;
  225. if (priv->rx_dirty_desc == priv->rx_ring_size - 1) {
  226. len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
  227. priv->rx_dirty_desc = 0;
  228. } else {
  229. priv->rx_dirty_desc++;
  230. }
  231. wmb();
  232. desc->len_stat = len_stat;
  233. priv->rx_desc_count++;
  234. /* tell dma engine we allocated one buffer */
  235. if (priv->dma_has_sram)
  236. enet_dma_writel(priv, 1, ENETDMA_BUFALLOC_REG(priv->rx_chan));
  237. else
  238. enet_dmac_writel(priv, 1, ENETDMAC_BUFALLOC, priv->rx_chan);
  239. }
  240. /* If rx ring is still empty, set a timer to try allocating
  241. * again at a later time. */
  242. if (priv->rx_desc_count == 0 && netif_running(dev)) {
  243. dev_warn(&priv->pdev->dev, "unable to refill rx ring\n");
  244. priv->rx_timeout.expires = jiffies + HZ;
  245. add_timer(&priv->rx_timeout);
  246. }
  247. return 0;
  248. }
  249. /*
  250. * timer callback to defer refill rx queue in case we're OOM
  251. */
  252. static void bcm_enet_refill_rx_timer(unsigned long data)
  253. {
  254. struct net_device *dev;
  255. struct bcm_enet_priv *priv;
  256. dev = (struct net_device *)data;
  257. priv = netdev_priv(dev);
  258. spin_lock(&priv->rx_lock);
  259. bcm_enet_refill_rx((struct net_device *)data);
  260. spin_unlock(&priv->rx_lock);
  261. }
  262. /*
  263. * extract packet from rx queue
  264. */
  265. static int bcm_enet_receive_queue(struct net_device *dev, int budget)
  266. {
  267. struct bcm_enet_priv *priv;
  268. struct device *kdev;
  269. int processed;
  270. priv = netdev_priv(dev);
  271. kdev = &priv->pdev->dev;
  272. processed = 0;
  273. /* don't scan ring further than number of refilled
  274. * descriptor */
  275. if (budget > priv->rx_desc_count)
  276. budget = priv->rx_desc_count;
  277. do {
  278. struct bcm_enet_desc *desc;
  279. struct sk_buff *skb;
  280. int desc_idx;
  281. u32 len_stat;
  282. unsigned int len;
  283. desc_idx = priv->rx_curr_desc;
  284. desc = &priv->rx_desc_cpu[desc_idx];
  285. /* make sure we actually read the descriptor status at
  286. * each loop */
  287. rmb();
  288. len_stat = desc->len_stat;
  289. /* break if dma ownership belongs to hw */
  290. if (len_stat & DMADESC_OWNER_MASK)
  291. break;
  292. processed++;
  293. priv->rx_curr_desc++;
  294. if (priv->rx_curr_desc == priv->rx_ring_size)
  295. priv->rx_curr_desc = 0;
  296. priv->rx_desc_count--;
  297. /* if the packet does not have start of packet _and_
  298. * end of packet flag set, then just recycle it */
  299. if ((len_stat & (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) !=
  300. (DMADESC_ESOP_MASK >> priv->dma_desc_shift)) {
  301. dev->stats.rx_dropped++;
  302. continue;
  303. }
  304. /* recycle packet if it's marked as bad */
  305. if (!priv->enet_is_sw &&
  306. unlikely(len_stat & DMADESC_ERR_MASK)) {
  307. dev->stats.rx_errors++;
  308. if (len_stat & DMADESC_OVSIZE_MASK)
  309. dev->stats.rx_length_errors++;
  310. if (len_stat & DMADESC_CRC_MASK)
  311. dev->stats.rx_crc_errors++;
  312. if (len_stat & DMADESC_UNDER_MASK)
  313. dev->stats.rx_frame_errors++;
  314. if (len_stat & DMADESC_OV_MASK)
  315. dev->stats.rx_fifo_errors++;
  316. continue;
  317. }
  318. /* valid packet */
  319. skb = priv->rx_skb[desc_idx];
  320. len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
  321. /* don't include FCS */
  322. len -= 4;
  323. if (len < copybreak) {
  324. struct sk_buff *nskb;
  325. nskb = napi_alloc_skb(&priv->napi, len);
  326. if (!nskb) {
  327. /* forget packet, just rearm desc */
  328. dev->stats.rx_dropped++;
  329. continue;
  330. }
  331. dma_sync_single_for_cpu(kdev, desc->address,
  332. len, DMA_FROM_DEVICE);
  333. memcpy(nskb->data, skb->data, len);
  334. dma_sync_single_for_device(kdev, desc->address,
  335. len, DMA_FROM_DEVICE);
  336. skb = nskb;
  337. } else {
  338. dma_unmap_single(&priv->pdev->dev, desc->address,
  339. priv->rx_skb_size, DMA_FROM_DEVICE);
  340. priv->rx_skb[desc_idx] = NULL;
  341. }
  342. skb_put(skb, len);
  343. skb->protocol = eth_type_trans(skb, dev);
  344. dev->stats.rx_packets++;
  345. dev->stats.rx_bytes += len;
  346. netif_receive_skb(skb);
  347. } while (--budget > 0);
  348. if (processed || !priv->rx_desc_count) {
  349. bcm_enet_refill_rx(dev);
  350. /* kick rx dma */
  351. enet_dmac_writel(priv, priv->dma_chan_en_mask,
  352. ENETDMAC_CHANCFG, priv->rx_chan);
  353. }
  354. return processed;
  355. }
  356. /*
  357. * try to or force reclaim of transmitted buffers
  358. */
  359. static int bcm_enet_tx_reclaim(struct net_device *dev, int force)
  360. {
  361. struct bcm_enet_priv *priv;
  362. int released;
  363. priv = netdev_priv(dev);
  364. released = 0;
  365. while (priv->tx_desc_count < priv->tx_ring_size) {
  366. struct bcm_enet_desc *desc;
  367. struct sk_buff *skb;
  368. /* We run in a bh and fight against start_xmit, which
  369. * is called with bh disabled */
  370. spin_lock(&priv->tx_lock);
  371. desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
  372. if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
  373. spin_unlock(&priv->tx_lock);
  374. break;
  375. }
  376. /* ensure other field of the descriptor were not read
  377. * before we checked ownership */
  378. rmb();
  379. skb = priv->tx_skb[priv->tx_dirty_desc];
  380. priv->tx_skb[priv->tx_dirty_desc] = NULL;
  381. dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
  382. DMA_TO_DEVICE);
  383. priv->tx_dirty_desc++;
  384. if (priv->tx_dirty_desc == priv->tx_ring_size)
  385. priv->tx_dirty_desc = 0;
  386. priv->tx_desc_count++;
  387. spin_unlock(&priv->tx_lock);
  388. if (desc->len_stat & DMADESC_UNDER_MASK)
  389. dev->stats.tx_errors++;
  390. dev_kfree_skb(skb);
  391. released++;
  392. }
  393. if (netif_queue_stopped(dev) && released)
  394. netif_wake_queue(dev);
  395. return released;
  396. }
  397. /*
  398. * poll func, called by network core
  399. */
  400. static int bcm_enet_poll(struct napi_struct *napi, int budget)
  401. {
  402. struct bcm_enet_priv *priv;
  403. struct net_device *dev;
  404. int rx_work_done;
  405. priv = container_of(napi, struct bcm_enet_priv, napi);
  406. dev = priv->net_dev;
  407. /* ack interrupts */
  408. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  409. ENETDMAC_IR, priv->rx_chan);
  410. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  411. ENETDMAC_IR, priv->tx_chan);
  412. /* reclaim sent skb */
  413. bcm_enet_tx_reclaim(dev, 0);
  414. spin_lock(&priv->rx_lock);
  415. rx_work_done = bcm_enet_receive_queue(dev, budget);
  416. spin_unlock(&priv->rx_lock);
  417. if (rx_work_done >= budget) {
  418. /* rx queue is not yet empty/clean */
  419. return rx_work_done;
  420. }
  421. /* no more packet in rx/tx queue, remove device from poll
  422. * queue */
  423. napi_complete(napi);
  424. /* restore rx/tx interrupt */
  425. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  426. ENETDMAC_IRMASK, priv->rx_chan);
  427. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  428. ENETDMAC_IRMASK, priv->tx_chan);
  429. return rx_work_done;
  430. }
  431. /*
  432. * mac interrupt handler
  433. */
  434. static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
  435. {
  436. struct net_device *dev;
  437. struct bcm_enet_priv *priv;
  438. u32 stat;
  439. dev = dev_id;
  440. priv = netdev_priv(dev);
  441. stat = enet_readl(priv, ENET_IR_REG);
  442. if (!(stat & ENET_IR_MIB))
  443. return IRQ_NONE;
  444. /* clear & mask interrupt */
  445. enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
  446. enet_writel(priv, 0, ENET_IRMASK_REG);
  447. /* read mib registers in workqueue */
  448. schedule_work(&priv->mib_update_task);
  449. return IRQ_HANDLED;
  450. }
  451. /*
  452. * rx/tx dma interrupt handler
  453. */
  454. static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
  455. {
  456. struct net_device *dev;
  457. struct bcm_enet_priv *priv;
  458. dev = dev_id;
  459. priv = netdev_priv(dev);
  460. /* mask rx/tx interrupts */
  461. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  462. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  463. napi_schedule(&priv->napi);
  464. return IRQ_HANDLED;
  465. }
  466. /*
  467. * tx request callback
  468. */
  469. static int bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  470. {
  471. struct bcm_enet_priv *priv;
  472. struct bcm_enet_desc *desc;
  473. u32 len_stat;
  474. int ret;
  475. priv = netdev_priv(dev);
  476. /* lock against tx reclaim */
  477. spin_lock(&priv->tx_lock);
  478. /* make sure the tx hw queue is not full, should not happen
  479. * since we stop queue before it's the case */
  480. if (unlikely(!priv->tx_desc_count)) {
  481. netif_stop_queue(dev);
  482. dev_err(&priv->pdev->dev, "xmit called with no tx desc "
  483. "available?\n");
  484. ret = NETDEV_TX_BUSY;
  485. goto out_unlock;
  486. }
  487. /* pad small packets sent on a switch device */
  488. if (priv->enet_is_sw && skb->len < 64) {
  489. int needed = 64 - skb->len;
  490. char *data;
  491. if (unlikely(skb_tailroom(skb) < needed)) {
  492. struct sk_buff *nskb;
  493. nskb = skb_copy_expand(skb, 0, needed, GFP_ATOMIC);
  494. if (!nskb) {
  495. ret = NETDEV_TX_BUSY;
  496. goto out_unlock;
  497. }
  498. dev_kfree_skb(skb);
  499. skb = nskb;
  500. }
  501. data = skb_put(skb, needed);
  502. memset(data, 0, needed);
  503. }
  504. /* point to the next available desc */
  505. desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
  506. priv->tx_skb[priv->tx_curr_desc] = skb;
  507. /* fill descriptor */
  508. desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
  509. DMA_TO_DEVICE);
  510. len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
  511. len_stat |= (DMADESC_ESOP_MASK >> priv->dma_desc_shift) |
  512. DMADESC_APPEND_CRC |
  513. DMADESC_OWNER_MASK;
  514. priv->tx_curr_desc++;
  515. if (priv->tx_curr_desc == priv->tx_ring_size) {
  516. priv->tx_curr_desc = 0;
  517. len_stat |= (DMADESC_WRAP_MASK >> priv->dma_desc_shift);
  518. }
  519. priv->tx_desc_count--;
  520. /* dma might be already polling, make sure we update desc
  521. * fields in correct order */
  522. wmb();
  523. desc->len_stat = len_stat;
  524. wmb();
  525. /* kick tx dma */
  526. enet_dmac_writel(priv, priv->dma_chan_en_mask,
  527. ENETDMAC_CHANCFG, priv->tx_chan);
  528. /* stop queue if no more desc available */
  529. if (!priv->tx_desc_count)
  530. netif_stop_queue(dev);
  531. dev->stats.tx_bytes += skb->len;
  532. dev->stats.tx_packets++;
  533. ret = NETDEV_TX_OK;
  534. out_unlock:
  535. spin_unlock(&priv->tx_lock);
  536. return ret;
  537. }
  538. /*
  539. * Change the interface's mac address.
  540. */
  541. static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
  542. {
  543. struct bcm_enet_priv *priv;
  544. struct sockaddr *addr = p;
  545. u32 val;
  546. priv = netdev_priv(dev);
  547. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  548. /* use perfect match register 0 to store my mac address */
  549. val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
  550. (dev->dev_addr[4] << 8) | dev->dev_addr[5];
  551. enet_writel(priv, val, ENET_PML_REG(0));
  552. val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  553. val |= ENET_PMH_DATAVALID_MASK;
  554. enet_writel(priv, val, ENET_PMH_REG(0));
  555. return 0;
  556. }
  557. /*
  558. * Change rx mode (promiscuous/allmulti) and update multicast list
  559. */
  560. static void bcm_enet_set_multicast_list(struct net_device *dev)
  561. {
  562. struct bcm_enet_priv *priv;
  563. struct netdev_hw_addr *ha;
  564. u32 val;
  565. int i;
  566. priv = netdev_priv(dev);
  567. val = enet_readl(priv, ENET_RXCFG_REG);
  568. if (dev->flags & IFF_PROMISC)
  569. val |= ENET_RXCFG_PROMISC_MASK;
  570. else
  571. val &= ~ENET_RXCFG_PROMISC_MASK;
  572. /* only 3 perfect match registers left, first one is used for
  573. * own mac address */
  574. if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
  575. val |= ENET_RXCFG_ALLMCAST_MASK;
  576. else
  577. val &= ~ENET_RXCFG_ALLMCAST_MASK;
  578. /* no need to set perfect match registers if we catch all
  579. * multicast */
  580. if (val & ENET_RXCFG_ALLMCAST_MASK) {
  581. enet_writel(priv, val, ENET_RXCFG_REG);
  582. return;
  583. }
  584. i = 0;
  585. netdev_for_each_mc_addr(ha, dev) {
  586. u8 *dmi_addr;
  587. u32 tmp;
  588. if (i == 3)
  589. break;
  590. /* update perfect match registers */
  591. dmi_addr = ha->addr;
  592. tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
  593. (dmi_addr[4] << 8) | dmi_addr[5];
  594. enet_writel(priv, tmp, ENET_PML_REG(i + 1));
  595. tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
  596. tmp |= ENET_PMH_DATAVALID_MASK;
  597. enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
  598. }
  599. for (; i < 3; i++) {
  600. enet_writel(priv, 0, ENET_PML_REG(i + 1));
  601. enet_writel(priv, 0, ENET_PMH_REG(i + 1));
  602. }
  603. enet_writel(priv, val, ENET_RXCFG_REG);
  604. }
  605. /*
  606. * set mac duplex parameters
  607. */
  608. static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
  609. {
  610. u32 val;
  611. val = enet_readl(priv, ENET_TXCTL_REG);
  612. if (fullduplex)
  613. val |= ENET_TXCTL_FD_MASK;
  614. else
  615. val &= ~ENET_TXCTL_FD_MASK;
  616. enet_writel(priv, val, ENET_TXCTL_REG);
  617. }
  618. /*
  619. * set mac flow control parameters
  620. */
  621. static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
  622. {
  623. u32 val;
  624. /* rx flow control (pause frame handling) */
  625. val = enet_readl(priv, ENET_RXCFG_REG);
  626. if (rx_en)
  627. val |= ENET_RXCFG_ENFLOW_MASK;
  628. else
  629. val &= ~ENET_RXCFG_ENFLOW_MASK;
  630. enet_writel(priv, val, ENET_RXCFG_REG);
  631. if (!priv->dma_has_sram)
  632. return;
  633. /* tx flow control (pause frame generation) */
  634. val = enet_dma_readl(priv, ENETDMA_CFG_REG);
  635. if (tx_en)
  636. val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
  637. else
  638. val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
  639. enet_dma_writel(priv, val, ENETDMA_CFG_REG);
  640. }
  641. /*
  642. * link changed callback (from phylib)
  643. */
  644. static void bcm_enet_adjust_phy_link(struct net_device *dev)
  645. {
  646. struct bcm_enet_priv *priv;
  647. struct phy_device *phydev;
  648. int status_changed;
  649. priv = netdev_priv(dev);
  650. phydev = priv->phydev;
  651. status_changed = 0;
  652. if (priv->old_link != phydev->link) {
  653. status_changed = 1;
  654. priv->old_link = phydev->link;
  655. }
  656. /* reflect duplex change in mac configuration */
  657. if (phydev->link && phydev->duplex != priv->old_duplex) {
  658. bcm_enet_set_duplex(priv,
  659. (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
  660. status_changed = 1;
  661. priv->old_duplex = phydev->duplex;
  662. }
  663. /* enable flow control if remote advertise it (trust phylib to
  664. * check that duplex is full */
  665. if (phydev->link && phydev->pause != priv->old_pause) {
  666. int rx_pause_en, tx_pause_en;
  667. if (phydev->pause) {
  668. /* pause was advertised by lpa and us */
  669. rx_pause_en = 1;
  670. tx_pause_en = 1;
  671. } else if (!priv->pause_auto) {
  672. /* pause setting overrided by user */
  673. rx_pause_en = priv->pause_rx;
  674. tx_pause_en = priv->pause_tx;
  675. } else {
  676. rx_pause_en = 0;
  677. tx_pause_en = 0;
  678. }
  679. bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
  680. status_changed = 1;
  681. priv->old_pause = phydev->pause;
  682. }
  683. if (status_changed) {
  684. pr_info("%s: link %s", dev->name, phydev->link ?
  685. "UP" : "DOWN");
  686. if (phydev->link)
  687. pr_cont(" - %d/%s - flow control %s", phydev->speed,
  688. DUPLEX_FULL == phydev->duplex ? "full" : "half",
  689. phydev->pause == 1 ? "rx&tx" : "off");
  690. pr_cont("\n");
  691. }
  692. }
  693. /*
  694. * link changed callback (if phylib is not used)
  695. */
  696. static void bcm_enet_adjust_link(struct net_device *dev)
  697. {
  698. struct bcm_enet_priv *priv;
  699. priv = netdev_priv(dev);
  700. bcm_enet_set_duplex(priv, priv->force_duplex_full);
  701. bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
  702. netif_carrier_on(dev);
  703. pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
  704. dev->name,
  705. priv->force_speed_100 ? 100 : 10,
  706. priv->force_duplex_full ? "full" : "half",
  707. priv->pause_rx ? "rx" : "off",
  708. priv->pause_tx ? "tx" : "off");
  709. }
  710. /*
  711. * open callback, allocate dma rings & buffers and start rx operation
  712. */
  713. static int bcm_enet_open(struct net_device *dev)
  714. {
  715. struct bcm_enet_priv *priv;
  716. struct sockaddr addr;
  717. struct device *kdev;
  718. struct phy_device *phydev;
  719. int i, ret;
  720. unsigned int size;
  721. char phy_id[MII_BUS_ID_SIZE + 3];
  722. void *p;
  723. u32 val;
  724. priv = netdev_priv(dev);
  725. kdev = &priv->pdev->dev;
  726. if (priv->has_phy) {
  727. /* connect to PHY */
  728. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  729. priv->mii_bus->id, priv->phy_id);
  730. phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
  731. PHY_INTERFACE_MODE_MII);
  732. if (IS_ERR(phydev)) {
  733. dev_err(kdev, "could not attach to PHY\n");
  734. return PTR_ERR(phydev);
  735. }
  736. /* mask with MAC supported features */
  737. phydev->supported &= (SUPPORTED_10baseT_Half |
  738. SUPPORTED_10baseT_Full |
  739. SUPPORTED_100baseT_Half |
  740. SUPPORTED_100baseT_Full |
  741. SUPPORTED_Autoneg |
  742. SUPPORTED_Pause |
  743. SUPPORTED_MII);
  744. phydev->advertising = phydev->supported;
  745. if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
  746. phydev->advertising |= SUPPORTED_Pause;
  747. else
  748. phydev->advertising &= ~SUPPORTED_Pause;
  749. dev_info(kdev, "attached PHY at address %d [%s]\n",
  750. phydev->addr, phydev->drv->name);
  751. priv->old_link = 0;
  752. priv->old_duplex = -1;
  753. priv->old_pause = -1;
  754. priv->phydev = phydev;
  755. }
  756. /* mask all interrupts and request them */
  757. enet_writel(priv, 0, ENET_IRMASK_REG);
  758. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  759. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  760. ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
  761. if (ret)
  762. goto out_phy_disconnect;
  763. ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, 0,
  764. dev->name, dev);
  765. if (ret)
  766. goto out_freeirq;
  767. ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
  768. 0, dev->name, dev);
  769. if (ret)
  770. goto out_freeirq_rx;
  771. /* initialize perfect match registers */
  772. for (i = 0; i < 4; i++) {
  773. enet_writel(priv, 0, ENET_PML_REG(i));
  774. enet_writel(priv, 0, ENET_PMH_REG(i));
  775. }
  776. /* write device mac address */
  777. memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
  778. bcm_enet_set_mac_address(dev, &addr);
  779. /* allocate rx dma ring */
  780. size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
  781. p = dma_zalloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
  782. if (!p) {
  783. ret = -ENOMEM;
  784. goto out_freeirq_tx;
  785. }
  786. priv->rx_desc_alloc_size = size;
  787. priv->rx_desc_cpu = p;
  788. /* allocate tx dma ring */
  789. size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
  790. p = dma_zalloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
  791. if (!p) {
  792. ret = -ENOMEM;
  793. goto out_free_rx_ring;
  794. }
  795. priv->tx_desc_alloc_size = size;
  796. priv->tx_desc_cpu = p;
  797. priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
  798. GFP_KERNEL);
  799. if (!priv->tx_skb) {
  800. ret = -ENOMEM;
  801. goto out_free_tx_ring;
  802. }
  803. priv->tx_desc_count = priv->tx_ring_size;
  804. priv->tx_dirty_desc = 0;
  805. priv->tx_curr_desc = 0;
  806. spin_lock_init(&priv->tx_lock);
  807. /* init & fill rx ring with skbs */
  808. priv->rx_skb = kcalloc(priv->rx_ring_size, sizeof(struct sk_buff *),
  809. GFP_KERNEL);
  810. if (!priv->rx_skb) {
  811. ret = -ENOMEM;
  812. goto out_free_tx_skb;
  813. }
  814. priv->rx_desc_count = 0;
  815. priv->rx_dirty_desc = 0;
  816. priv->rx_curr_desc = 0;
  817. /* initialize flow control buffer allocation */
  818. if (priv->dma_has_sram)
  819. enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  820. ENETDMA_BUFALLOC_REG(priv->rx_chan));
  821. else
  822. enet_dmac_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  823. ENETDMAC_BUFALLOC, priv->rx_chan);
  824. if (bcm_enet_refill_rx(dev)) {
  825. dev_err(kdev, "cannot allocate rx skb queue\n");
  826. ret = -ENOMEM;
  827. goto out;
  828. }
  829. /* write rx & tx ring addresses */
  830. if (priv->dma_has_sram) {
  831. enet_dmas_writel(priv, priv->rx_desc_dma,
  832. ENETDMAS_RSTART_REG, priv->rx_chan);
  833. enet_dmas_writel(priv, priv->tx_desc_dma,
  834. ENETDMAS_RSTART_REG, priv->tx_chan);
  835. } else {
  836. enet_dmac_writel(priv, priv->rx_desc_dma,
  837. ENETDMAC_RSTART, priv->rx_chan);
  838. enet_dmac_writel(priv, priv->tx_desc_dma,
  839. ENETDMAC_RSTART, priv->tx_chan);
  840. }
  841. /* clear remaining state ram for rx & tx channel */
  842. if (priv->dma_has_sram) {
  843. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
  844. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
  845. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
  846. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
  847. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
  848. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
  849. } else {
  850. enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->rx_chan);
  851. enet_dmac_writel(priv, 0, ENETDMAC_FC, priv->tx_chan);
  852. }
  853. /* set max rx/tx length */
  854. enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
  855. enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
  856. /* set dma maximum burst len */
  857. enet_dmac_writel(priv, priv->dma_maxburst,
  858. ENETDMAC_MAXBURST, priv->rx_chan);
  859. enet_dmac_writel(priv, priv->dma_maxburst,
  860. ENETDMAC_MAXBURST, priv->tx_chan);
  861. /* set correct transmit fifo watermark */
  862. enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
  863. /* set flow control low/high threshold to 1/3 / 2/3 */
  864. if (priv->dma_has_sram) {
  865. val = priv->rx_ring_size / 3;
  866. enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
  867. val = (priv->rx_ring_size * 2) / 3;
  868. enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
  869. } else {
  870. enet_dmac_writel(priv, 5, ENETDMAC_FC, priv->rx_chan);
  871. enet_dmac_writel(priv, priv->rx_ring_size, ENETDMAC_LEN, priv->rx_chan);
  872. enet_dmac_writel(priv, priv->tx_ring_size, ENETDMAC_LEN, priv->tx_chan);
  873. }
  874. /* all set, enable mac and interrupts, start dma engine and
  875. * kick rx dma channel */
  876. wmb();
  877. val = enet_readl(priv, ENET_CTL_REG);
  878. val |= ENET_CTL_ENABLE_MASK;
  879. enet_writel(priv, val, ENET_CTL_REG);
  880. if (priv->dma_has_sram)
  881. enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  882. enet_dmac_writel(priv, priv->dma_chan_en_mask,
  883. ENETDMAC_CHANCFG, priv->rx_chan);
  884. /* watch "mib counters about to overflow" interrupt */
  885. enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
  886. enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
  887. /* watch "packet transferred" interrupt in rx and tx */
  888. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  889. ENETDMAC_IR, priv->rx_chan);
  890. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  891. ENETDMAC_IR, priv->tx_chan);
  892. /* make sure we enable napi before rx interrupt */
  893. napi_enable(&priv->napi);
  894. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  895. ENETDMAC_IRMASK, priv->rx_chan);
  896. enet_dmac_writel(priv, priv->dma_chan_int_mask,
  897. ENETDMAC_IRMASK, priv->tx_chan);
  898. if (priv->has_phy)
  899. phy_start(priv->phydev);
  900. else
  901. bcm_enet_adjust_link(dev);
  902. netif_start_queue(dev);
  903. return 0;
  904. out:
  905. for (i = 0; i < priv->rx_ring_size; i++) {
  906. struct bcm_enet_desc *desc;
  907. if (!priv->rx_skb[i])
  908. continue;
  909. desc = &priv->rx_desc_cpu[i];
  910. dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
  911. DMA_FROM_DEVICE);
  912. kfree_skb(priv->rx_skb[i]);
  913. }
  914. kfree(priv->rx_skb);
  915. out_free_tx_skb:
  916. kfree(priv->tx_skb);
  917. out_free_tx_ring:
  918. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  919. priv->tx_desc_cpu, priv->tx_desc_dma);
  920. out_free_rx_ring:
  921. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  922. priv->rx_desc_cpu, priv->rx_desc_dma);
  923. out_freeirq_tx:
  924. free_irq(priv->irq_tx, dev);
  925. out_freeirq_rx:
  926. free_irq(priv->irq_rx, dev);
  927. out_freeirq:
  928. free_irq(dev->irq, dev);
  929. out_phy_disconnect:
  930. phy_disconnect(priv->phydev);
  931. return ret;
  932. }
  933. /*
  934. * disable mac
  935. */
  936. static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
  937. {
  938. int limit;
  939. u32 val;
  940. val = enet_readl(priv, ENET_CTL_REG);
  941. val |= ENET_CTL_DISABLE_MASK;
  942. enet_writel(priv, val, ENET_CTL_REG);
  943. limit = 1000;
  944. do {
  945. u32 val;
  946. val = enet_readl(priv, ENET_CTL_REG);
  947. if (!(val & ENET_CTL_DISABLE_MASK))
  948. break;
  949. udelay(1);
  950. } while (limit--);
  951. }
  952. /*
  953. * disable dma in given channel
  954. */
  955. static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
  956. {
  957. int limit;
  958. enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG, chan);
  959. limit = 1000;
  960. do {
  961. u32 val;
  962. val = enet_dmac_readl(priv, ENETDMAC_CHANCFG, chan);
  963. if (!(val & ENETDMAC_CHANCFG_EN_MASK))
  964. break;
  965. udelay(1);
  966. } while (limit--);
  967. }
  968. /*
  969. * stop callback
  970. */
  971. static int bcm_enet_stop(struct net_device *dev)
  972. {
  973. struct bcm_enet_priv *priv;
  974. struct device *kdev;
  975. int i;
  976. priv = netdev_priv(dev);
  977. kdev = &priv->pdev->dev;
  978. netif_stop_queue(dev);
  979. napi_disable(&priv->napi);
  980. if (priv->has_phy)
  981. phy_stop(priv->phydev);
  982. del_timer_sync(&priv->rx_timeout);
  983. /* mask all interrupts */
  984. enet_writel(priv, 0, ENET_IRMASK_REG);
  985. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  986. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  987. /* make sure no mib update is scheduled */
  988. cancel_work_sync(&priv->mib_update_task);
  989. /* disable dma & mac */
  990. bcm_enet_disable_dma(priv, priv->tx_chan);
  991. bcm_enet_disable_dma(priv, priv->rx_chan);
  992. bcm_enet_disable_mac(priv);
  993. /* force reclaim of all tx buffers */
  994. bcm_enet_tx_reclaim(dev, 1);
  995. /* free the rx skb ring */
  996. for (i = 0; i < priv->rx_ring_size; i++) {
  997. struct bcm_enet_desc *desc;
  998. if (!priv->rx_skb[i])
  999. continue;
  1000. desc = &priv->rx_desc_cpu[i];
  1001. dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
  1002. DMA_FROM_DEVICE);
  1003. kfree_skb(priv->rx_skb[i]);
  1004. }
  1005. /* free remaining allocated memory */
  1006. kfree(priv->rx_skb);
  1007. kfree(priv->tx_skb);
  1008. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  1009. priv->rx_desc_cpu, priv->rx_desc_dma);
  1010. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1011. priv->tx_desc_cpu, priv->tx_desc_dma);
  1012. free_irq(priv->irq_tx, dev);
  1013. free_irq(priv->irq_rx, dev);
  1014. free_irq(dev->irq, dev);
  1015. /* release phy */
  1016. if (priv->has_phy) {
  1017. phy_disconnect(priv->phydev);
  1018. priv->phydev = NULL;
  1019. }
  1020. return 0;
  1021. }
  1022. /*
  1023. * ethtool callbacks
  1024. */
  1025. struct bcm_enet_stats {
  1026. char stat_string[ETH_GSTRING_LEN];
  1027. int sizeof_stat;
  1028. int stat_offset;
  1029. int mib_reg;
  1030. };
  1031. #define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
  1032. offsetof(struct bcm_enet_priv, m)
  1033. #define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
  1034. offsetof(struct net_device_stats, m)
  1035. static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
  1036. { "rx_packets", DEV_STAT(rx_packets), -1 },
  1037. { "tx_packets", DEV_STAT(tx_packets), -1 },
  1038. { "rx_bytes", DEV_STAT(rx_bytes), -1 },
  1039. { "tx_bytes", DEV_STAT(tx_bytes), -1 },
  1040. { "rx_errors", DEV_STAT(rx_errors), -1 },
  1041. { "tx_errors", DEV_STAT(tx_errors), -1 },
  1042. { "rx_dropped", DEV_STAT(rx_dropped), -1 },
  1043. { "tx_dropped", DEV_STAT(tx_dropped), -1 },
  1044. { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
  1045. { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
  1046. { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
  1047. { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
  1048. { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
  1049. { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
  1050. { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
  1051. { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
  1052. { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
  1053. { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
  1054. { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
  1055. { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
  1056. { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
  1057. { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
  1058. { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
  1059. { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
  1060. { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
  1061. { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
  1062. { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
  1063. { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
  1064. { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
  1065. { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
  1066. { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
  1067. { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
  1068. { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
  1069. { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
  1070. { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
  1071. { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
  1072. { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
  1073. { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
  1074. { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
  1075. { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
  1076. { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
  1077. { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
  1078. { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
  1079. { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
  1080. { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
  1081. { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
  1082. { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
  1083. { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
  1084. { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
  1085. { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
  1086. { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
  1087. };
  1088. #define BCM_ENET_STATS_LEN ARRAY_SIZE(bcm_enet_gstrings_stats)
  1089. static const u32 unused_mib_regs[] = {
  1090. ETH_MIB_TX_ALL_OCTETS,
  1091. ETH_MIB_TX_ALL_PKTS,
  1092. ETH_MIB_RX_ALL_OCTETS,
  1093. ETH_MIB_RX_ALL_PKTS,
  1094. };
  1095. static void bcm_enet_get_drvinfo(struct net_device *netdev,
  1096. struct ethtool_drvinfo *drvinfo)
  1097. {
  1098. strlcpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
  1099. strlcpy(drvinfo->version, bcm_enet_driver_version,
  1100. sizeof(drvinfo->version));
  1101. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1102. strlcpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
  1103. }
  1104. static int bcm_enet_get_sset_count(struct net_device *netdev,
  1105. int string_set)
  1106. {
  1107. switch (string_set) {
  1108. case ETH_SS_STATS:
  1109. return BCM_ENET_STATS_LEN;
  1110. default:
  1111. return -EINVAL;
  1112. }
  1113. }
  1114. static void bcm_enet_get_strings(struct net_device *netdev,
  1115. u32 stringset, u8 *data)
  1116. {
  1117. int i;
  1118. switch (stringset) {
  1119. case ETH_SS_STATS:
  1120. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1121. memcpy(data + i * ETH_GSTRING_LEN,
  1122. bcm_enet_gstrings_stats[i].stat_string,
  1123. ETH_GSTRING_LEN);
  1124. }
  1125. break;
  1126. }
  1127. }
  1128. static void update_mib_counters(struct bcm_enet_priv *priv)
  1129. {
  1130. int i;
  1131. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1132. const struct bcm_enet_stats *s;
  1133. u32 val;
  1134. char *p;
  1135. s = &bcm_enet_gstrings_stats[i];
  1136. if (s->mib_reg == -1)
  1137. continue;
  1138. val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
  1139. p = (char *)priv + s->stat_offset;
  1140. if (s->sizeof_stat == sizeof(u64))
  1141. *(u64 *)p += val;
  1142. else
  1143. *(u32 *)p += val;
  1144. }
  1145. /* also empty unused mib counters to make sure mib counter
  1146. * overflow interrupt is cleared */
  1147. for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
  1148. (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
  1149. }
  1150. static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
  1151. {
  1152. struct bcm_enet_priv *priv;
  1153. priv = container_of(t, struct bcm_enet_priv, mib_update_task);
  1154. mutex_lock(&priv->mib_update_lock);
  1155. update_mib_counters(priv);
  1156. mutex_unlock(&priv->mib_update_lock);
  1157. /* reenable mib interrupt */
  1158. if (netif_running(priv->net_dev))
  1159. enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
  1160. }
  1161. static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
  1162. struct ethtool_stats *stats,
  1163. u64 *data)
  1164. {
  1165. struct bcm_enet_priv *priv;
  1166. int i;
  1167. priv = netdev_priv(netdev);
  1168. mutex_lock(&priv->mib_update_lock);
  1169. update_mib_counters(priv);
  1170. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1171. const struct bcm_enet_stats *s;
  1172. char *p;
  1173. s = &bcm_enet_gstrings_stats[i];
  1174. if (s->mib_reg == -1)
  1175. p = (char *)&netdev->stats;
  1176. else
  1177. p = (char *)priv;
  1178. p += s->stat_offset;
  1179. data[i] = (s->sizeof_stat == sizeof(u64)) ?
  1180. *(u64 *)p : *(u32 *)p;
  1181. }
  1182. mutex_unlock(&priv->mib_update_lock);
  1183. }
  1184. static int bcm_enet_nway_reset(struct net_device *dev)
  1185. {
  1186. struct bcm_enet_priv *priv;
  1187. priv = netdev_priv(dev);
  1188. if (priv->has_phy) {
  1189. if (!priv->phydev)
  1190. return -ENODEV;
  1191. return genphy_restart_aneg(priv->phydev);
  1192. }
  1193. return -EOPNOTSUPP;
  1194. }
  1195. static int bcm_enet_get_settings(struct net_device *dev,
  1196. struct ethtool_cmd *cmd)
  1197. {
  1198. struct bcm_enet_priv *priv;
  1199. priv = netdev_priv(dev);
  1200. cmd->maxrxpkt = 0;
  1201. cmd->maxtxpkt = 0;
  1202. if (priv->has_phy) {
  1203. if (!priv->phydev)
  1204. return -ENODEV;
  1205. return phy_ethtool_gset(priv->phydev, cmd);
  1206. } else {
  1207. cmd->autoneg = 0;
  1208. ethtool_cmd_speed_set(cmd, ((priv->force_speed_100)
  1209. ? SPEED_100 : SPEED_10));
  1210. cmd->duplex = (priv->force_duplex_full) ?
  1211. DUPLEX_FULL : DUPLEX_HALF;
  1212. cmd->supported = ADVERTISED_10baseT_Half |
  1213. ADVERTISED_10baseT_Full |
  1214. ADVERTISED_100baseT_Half |
  1215. ADVERTISED_100baseT_Full;
  1216. cmd->advertising = 0;
  1217. cmd->port = PORT_MII;
  1218. cmd->transceiver = XCVR_EXTERNAL;
  1219. }
  1220. return 0;
  1221. }
  1222. static int bcm_enet_set_settings(struct net_device *dev,
  1223. struct ethtool_cmd *cmd)
  1224. {
  1225. struct bcm_enet_priv *priv;
  1226. priv = netdev_priv(dev);
  1227. if (priv->has_phy) {
  1228. if (!priv->phydev)
  1229. return -ENODEV;
  1230. return phy_ethtool_sset(priv->phydev, cmd);
  1231. } else {
  1232. if (cmd->autoneg ||
  1233. (cmd->speed != SPEED_100 && cmd->speed != SPEED_10) ||
  1234. cmd->port != PORT_MII)
  1235. return -EINVAL;
  1236. priv->force_speed_100 = (cmd->speed == SPEED_100) ? 1 : 0;
  1237. priv->force_duplex_full = (cmd->duplex == DUPLEX_FULL) ? 1 : 0;
  1238. if (netif_running(dev))
  1239. bcm_enet_adjust_link(dev);
  1240. return 0;
  1241. }
  1242. }
  1243. static void bcm_enet_get_ringparam(struct net_device *dev,
  1244. struct ethtool_ringparam *ering)
  1245. {
  1246. struct bcm_enet_priv *priv;
  1247. priv = netdev_priv(dev);
  1248. /* rx/tx ring is actually only limited by memory */
  1249. ering->rx_max_pending = 8192;
  1250. ering->tx_max_pending = 8192;
  1251. ering->rx_pending = priv->rx_ring_size;
  1252. ering->tx_pending = priv->tx_ring_size;
  1253. }
  1254. static int bcm_enet_set_ringparam(struct net_device *dev,
  1255. struct ethtool_ringparam *ering)
  1256. {
  1257. struct bcm_enet_priv *priv;
  1258. int was_running;
  1259. priv = netdev_priv(dev);
  1260. was_running = 0;
  1261. if (netif_running(dev)) {
  1262. bcm_enet_stop(dev);
  1263. was_running = 1;
  1264. }
  1265. priv->rx_ring_size = ering->rx_pending;
  1266. priv->tx_ring_size = ering->tx_pending;
  1267. if (was_running) {
  1268. int err;
  1269. err = bcm_enet_open(dev);
  1270. if (err)
  1271. dev_close(dev);
  1272. else
  1273. bcm_enet_set_multicast_list(dev);
  1274. }
  1275. return 0;
  1276. }
  1277. static void bcm_enet_get_pauseparam(struct net_device *dev,
  1278. struct ethtool_pauseparam *ecmd)
  1279. {
  1280. struct bcm_enet_priv *priv;
  1281. priv = netdev_priv(dev);
  1282. ecmd->autoneg = priv->pause_auto;
  1283. ecmd->rx_pause = priv->pause_rx;
  1284. ecmd->tx_pause = priv->pause_tx;
  1285. }
  1286. static int bcm_enet_set_pauseparam(struct net_device *dev,
  1287. struct ethtool_pauseparam *ecmd)
  1288. {
  1289. struct bcm_enet_priv *priv;
  1290. priv = netdev_priv(dev);
  1291. if (priv->has_phy) {
  1292. if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
  1293. /* asymetric pause mode not supported,
  1294. * actually possible but integrated PHY has RO
  1295. * asym_pause bit */
  1296. return -EINVAL;
  1297. }
  1298. } else {
  1299. /* no pause autoneg on direct mii connection */
  1300. if (ecmd->autoneg)
  1301. return -EINVAL;
  1302. }
  1303. priv->pause_auto = ecmd->autoneg;
  1304. priv->pause_rx = ecmd->rx_pause;
  1305. priv->pause_tx = ecmd->tx_pause;
  1306. return 0;
  1307. }
  1308. static const struct ethtool_ops bcm_enet_ethtool_ops = {
  1309. .get_strings = bcm_enet_get_strings,
  1310. .get_sset_count = bcm_enet_get_sset_count,
  1311. .get_ethtool_stats = bcm_enet_get_ethtool_stats,
  1312. .nway_reset = bcm_enet_nway_reset,
  1313. .get_settings = bcm_enet_get_settings,
  1314. .set_settings = bcm_enet_set_settings,
  1315. .get_drvinfo = bcm_enet_get_drvinfo,
  1316. .get_link = ethtool_op_get_link,
  1317. .get_ringparam = bcm_enet_get_ringparam,
  1318. .set_ringparam = bcm_enet_set_ringparam,
  1319. .get_pauseparam = bcm_enet_get_pauseparam,
  1320. .set_pauseparam = bcm_enet_set_pauseparam,
  1321. };
  1322. static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1323. {
  1324. struct bcm_enet_priv *priv;
  1325. priv = netdev_priv(dev);
  1326. if (priv->has_phy) {
  1327. if (!priv->phydev)
  1328. return -ENODEV;
  1329. return phy_mii_ioctl(priv->phydev, rq, cmd);
  1330. } else {
  1331. struct mii_if_info mii;
  1332. mii.dev = dev;
  1333. mii.mdio_read = bcm_enet_mdio_read_mii;
  1334. mii.mdio_write = bcm_enet_mdio_write_mii;
  1335. mii.phy_id = 0;
  1336. mii.phy_id_mask = 0x3f;
  1337. mii.reg_num_mask = 0x1f;
  1338. return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
  1339. }
  1340. }
  1341. /*
  1342. * calculate actual hardware mtu
  1343. */
  1344. static int compute_hw_mtu(struct bcm_enet_priv *priv, int mtu)
  1345. {
  1346. int actual_mtu;
  1347. actual_mtu = mtu;
  1348. /* add ethernet header + vlan tag size */
  1349. actual_mtu += VLAN_ETH_HLEN;
  1350. if (actual_mtu < 64 || actual_mtu > BCMENET_MAX_MTU)
  1351. return -EINVAL;
  1352. /*
  1353. * setup maximum size before we get overflow mark in
  1354. * descriptor, note that this will not prevent reception of
  1355. * big frames, they will be split into multiple buffers
  1356. * anyway
  1357. */
  1358. priv->hw_mtu = actual_mtu;
  1359. /*
  1360. * align rx buffer size to dma burst len, account FCS since
  1361. * it's appended
  1362. */
  1363. priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
  1364. priv->dma_maxburst * 4);
  1365. return 0;
  1366. }
  1367. /*
  1368. * adjust mtu, can't be called while device is running
  1369. */
  1370. static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
  1371. {
  1372. int ret;
  1373. if (netif_running(dev))
  1374. return -EBUSY;
  1375. ret = compute_hw_mtu(netdev_priv(dev), new_mtu);
  1376. if (ret)
  1377. return ret;
  1378. dev->mtu = new_mtu;
  1379. return 0;
  1380. }
  1381. /*
  1382. * preinit hardware to allow mii operation while device is down
  1383. */
  1384. static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
  1385. {
  1386. u32 val;
  1387. int limit;
  1388. /* make sure mac is disabled */
  1389. bcm_enet_disable_mac(priv);
  1390. /* soft reset mac */
  1391. val = ENET_CTL_SRESET_MASK;
  1392. enet_writel(priv, val, ENET_CTL_REG);
  1393. wmb();
  1394. limit = 1000;
  1395. do {
  1396. val = enet_readl(priv, ENET_CTL_REG);
  1397. if (!(val & ENET_CTL_SRESET_MASK))
  1398. break;
  1399. udelay(1);
  1400. } while (limit--);
  1401. /* select correct mii interface */
  1402. val = enet_readl(priv, ENET_CTL_REG);
  1403. if (priv->use_external_mii)
  1404. val |= ENET_CTL_EPHYSEL_MASK;
  1405. else
  1406. val &= ~ENET_CTL_EPHYSEL_MASK;
  1407. enet_writel(priv, val, ENET_CTL_REG);
  1408. /* turn on mdc clock */
  1409. enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
  1410. ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
  1411. /* set mib counters to self-clear when read */
  1412. val = enet_readl(priv, ENET_MIBCTL_REG);
  1413. val |= ENET_MIBCTL_RDCLEAR_MASK;
  1414. enet_writel(priv, val, ENET_MIBCTL_REG);
  1415. }
  1416. static const struct net_device_ops bcm_enet_ops = {
  1417. .ndo_open = bcm_enet_open,
  1418. .ndo_stop = bcm_enet_stop,
  1419. .ndo_start_xmit = bcm_enet_start_xmit,
  1420. .ndo_set_mac_address = bcm_enet_set_mac_address,
  1421. .ndo_set_rx_mode = bcm_enet_set_multicast_list,
  1422. .ndo_do_ioctl = bcm_enet_ioctl,
  1423. .ndo_change_mtu = bcm_enet_change_mtu,
  1424. };
  1425. /*
  1426. * allocate netdevice, request register memory and register device.
  1427. */
  1428. static int bcm_enet_probe(struct platform_device *pdev)
  1429. {
  1430. struct bcm_enet_priv *priv;
  1431. struct net_device *dev;
  1432. struct bcm63xx_enet_platform_data *pd;
  1433. struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
  1434. struct mii_bus *bus;
  1435. const char *clk_name;
  1436. int i, ret;
  1437. /* stop if shared driver failed, assume driver->probe will be
  1438. * called in the same order we register devices (correct ?) */
  1439. if (!bcm_enet_shared_base[0])
  1440. return -ENODEV;
  1441. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1442. res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1443. res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  1444. if (!res_irq || !res_irq_rx || !res_irq_tx)
  1445. return -ENODEV;
  1446. ret = 0;
  1447. dev = alloc_etherdev(sizeof(*priv));
  1448. if (!dev)
  1449. return -ENOMEM;
  1450. priv = netdev_priv(dev);
  1451. priv->enet_is_sw = false;
  1452. priv->dma_maxburst = BCMENET_DMA_MAXBURST;
  1453. ret = compute_hw_mtu(priv, dev->mtu);
  1454. if (ret)
  1455. goto out;
  1456. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1457. priv->base = devm_ioremap_resource(&pdev->dev, res_mem);
  1458. if (IS_ERR(priv->base)) {
  1459. ret = PTR_ERR(priv->base);
  1460. goto out;
  1461. }
  1462. dev->irq = priv->irq = res_irq->start;
  1463. priv->irq_rx = res_irq_rx->start;
  1464. priv->irq_tx = res_irq_tx->start;
  1465. priv->mac_id = pdev->id;
  1466. /* get rx & tx dma channel id for this mac */
  1467. if (priv->mac_id == 0) {
  1468. priv->rx_chan = 0;
  1469. priv->tx_chan = 1;
  1470. clk_name = "enet0";
  1471. } else {
  1472. priv->rx_chan = 2;
  1473. priv->tx_chan = 3;
  1474. clk_name = "enet1";
  1475. }
  1476. priv->mac_clk = clk_get(&pdev->dev, clk_name);
  1477. if (IS_ERR(priv->mac_clk)) {
  1478. ret = PTR_ERR(priv->mac_clk);
  1479. goto out;
  1480. }
  1481. ret = clk_prepare_enable(priv->mac_clk);
  1482. if (ret)
  1483. goto out_put_clk_mac;
  1484. /* initialize default and fetch platform data */
  1485. priv->rx_ring_size = BCMENET_DEF_RX_DESC;
  1486. priv->tx_ring_size = BCMENET_DEF_TX_DESC;
  1487. pd = dev_get_platdata(&pdev->dev);
  1488. if (pd) {
  1489. memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
  1490. priv->has_phy = pd->has_phy;
  1491. priv->phy_id = pd->phy_id;
  1492. priv->has_phy_interrupt = pd->has_phy_interrupt;
  1493. priv->phy_interrupt = pd->phy_interrupt;
  1494. priv->use_external_mii = !pd->use_internal_phy;
  1495. priv->pause_auto = pd->pause_auto;
  1496. priv->pause_rx = pd->pause_rx;
  1497. priv->pause_tx = pd->pause_tx;
  1498. priv->force_duplex_full = pd->force_duplex_full;
  1499. priv->force_speed_100 = pd->force_speed_100;
  1500. priv->dma_chan_en_mask = pd->dma_chan_en_mask;
  1501. priv->dma_chan_int_mask = pd->dma_chan_int_mask;
  1502. priv->dma_chan_width = pd->dma_chan_width;
  1503. priv->dma_has_sram = pd->dma_has_sram;
  1504. priv->dma_desc_shift = pd->dma_desc_shift;
  1505. }
  1506. if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
  1507. /* using internal PHY, enable clock */
  1508. priv->phy_clk = clk_get(&pdev->dev, "ephy");
  1509. if (IS_ERR(priv->phy_clk)) {
  1510. ret = PTR_ERR(priv->phy_clk);
  1511. priv->phy_clk = NULL;
  1512. goto out_disable_clk_mac;
  1513. }
  1514. ret = clk_prepare_enable(priv->phy_clk);
  1515. if (ret)
  1516. goto out_put_clk_phy;
  1517. }
  1518. /* do minimal hardware init to be able to probe mii bus */
  1519. bcm_enet_hw_preinit(priv);
  1520. /* MII bus registration */
  1521. if (priv->has_phy) {
  1522. priv->mii_bus = mdiobus_alloc();
  1523. if (!priv->mii_bus) {
  1524. ret = -ENOMEM;
  1525. goto out_uninit_hw;
  1526. }
  1527. bus = priv->mii_bus;
  1528. bus->name = "bcm63xx_enet MII bus";
  1529. bus->parent = &pdev->dev;
  1530. bus->priv = priv;
  1531. bus->read = bcm_enet_mdio_read_phylib;
  1532. bus->write = bcm_enet_mdio_write_phylib;
  1533. sprintf(bus->id, "%s-%d", pdev->name, priv->mac_id);
  1534. /* only probe bus where we think the PHY is, because
  1535. * the mdio read operation return 0 instead of 0xffff
  1536. * if a slave is not present on hw */
  1537. bus->phy_mask = ~(1 << priv->phy_id);
  1538. bus->irq = devm_kzalloc(&pdev->dev, sizeof(int) * PHY_MAX_ADDR,
  1539. GFP_KERNEL);
  1540. if (!bus->irq) {
  1541. ret = -ENOMEM;
  1542. goto out_free_mdio;
  1543. }
  1544. if (priv->has_phy_interrupt)
  1545. bus->irq[priv->phy_id] = priv->phy_interrupt;
  1546. else
  1547. bus->irq[priv->phy_id] = PHY_POLL;
  1548. ret = mdiobus_register(bus);
  1549. if (ret) {
  1550. dev_err(&pdev->dev, "unable to register mdio bus\n");
  1551. goto out_free_mdio;
  1552. }
  1553. } else {
  1554. /* run platform code to initialize PHY device */
  1555. if (pd->mii_config &&
  1556. pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
  1557. bcm_enet_mdio_write_mii)) {
  1558. dev_err(&pdev->dev, "unable to configure mdio bus\n");
  1559. goto out_uninit_hw;
  1560. }
  1561. }
  1562. spin_lock_init(&priv->rx_lock);
  1563. /* init rx timeout (used for oom) */
  1564. init_timer(&priv->rx_timeout);
  1565. priv->rx_timeout.function = bcm_enet_refill_rx_timer;
  1566. priv->rx_timeout.data = (unsigned long)dev;
  1567. /* init the mib update lock&work */
  1568. mutex_init(&priv->mib_update_lock);
  1569. INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
  1570. /* zero mib counters */
  1571. for (i = 0; i < ENET_MIB_REG_COUNT; i++)
  1572. enet_writel(priv, 0, ENET_MIB_REG(i));
  1573. /* register netdevice */
  1574. dev->netdev_ops = &bcm_enet_ops;
  1575. netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
  1576. dev->ethtool_ops = &bcm_enet_ethtool_ops;
  1577. SET_NETDEV_DEV(dev, &pdev->dev);
  1578. ret = register_netdev(dev);
  1579. if (ret)
  1580. goto out_unregister_mdio;
  1581. netif_carrier_off(dev);
  1582. platform_set_drvdata(pdev, dev);
  1583. priv->pdev = pdev;
  1584. priv->net_dev = dev;
  1585. return 0;
  1586. out_unregister_mdio:
  1587. if (priv->mii_bus)
  1588. mdiobus_unregister(priv->mii_bus);
  1589. out_free_mdio:
  1590. if (priv->mii_bus)
  1591. mdiobus_free(priv->mii_bus);
  1592. out_uninit_hw:
  1593. /* turn off mdc clock */
  1594. enet_writel(priv, 0, ENET_MIISC_REG);
  1595. if (priv->phy_clk)
  1596. clk_disable_unprepare(priv->phy_clk);
  1597. out_put_clk_phy:
  1598. if (priv->phy_clk)
  1599. clk_put(priv->phy_clk);
  1600. out_disable_clk_mac:
  1601. clk_disable_unprepare(priv->mac_clk);
  1602. out_put_clk_mac:
  1603. clk_put(priv->mac_clk);
  1604. out:
  1605. free_netdev(dev);
  1606. return ret;
  1607. }
  1608. /*
  1609. * exit func, stops hardware and unregisters netdevice
  1610. */
  1611. static int bcm_enet_remove(struct platform_device *pdev)
  1612. {
  1613. struct bcm_enet_priv *priv;
  1614. struct net_device *dev;
  1615. /* stop netdevice */
  1616. dev = platform_get_drvdata(pdev);
  1617. priv = netdev_priv(dev);
  1618. unregister_netdev(dev);
  1619. /* turn off mdc clock */
  1620. enet_writel(priv, 0, ENET_MIISC_REG);
  1621. if (priv->has_phy) {
  1622. mdiobus_unregister(priv->mii_bus);
  1623. mdiobus_free(priv->mii_bus);
  1624. } else {
  1625. struct bcm63xx_enet_platform_data *pd;
  1626. pd = dev_get_platdata(&pdev->dev);
  1627. if (pd && pd->mii_config)
  1628. pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
  1629. bcm_enet_mdio_write_mii);
  1630. }
  1631. /* disable hw block clocks */
  1632. if (priv->phy_clk) {
  1633. clk_disable_unprepare(priv->phy_clk);
  1634. clk_put(priv->phy_clk);
  1635. }
  1636. clk_disable_unprepare(priv->mac_clk);
  1637. clk_put(priv->mac_clk);
  1638. free_netdev(dev);
  1639. return 0;
  1640. }
  1641. struct platform_driver bcm63xx_enet_driver = {
  1642. .probe = bcm_enet_probe,
  1643. .remove = bcm_enet_remove,
  1644. .driver = {
  1645. .name = "bcm63xx_enet",
  1646. .owner = THIS_MODULE,
  1647. },
  1648. };
  1649. /*
  1650. * switch mii access callbacks
  1651. */
  1652. static int bcmenet_sw_mdio_read(struct bcm_enet_priv *priv,
  1653. int ext, int phy_id, int location)
  1654. {
  1655. u32 reg;
  1656. int ret;
  1657. spin_lock_bh(&priv->enetsw_mdio_lock);
  1658. enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
  1659. reg = ENETSW_MDIOC_RD_MASK |
  1660. (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
  1661. (location << ENETSW_MDIOC_REG_SHIFT);
  1662. if (ext)
  1663. reg |= ENETSW_MDIOC_EXT_MASK;
  1664. enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
  1665. udelay(50);
  1666. ret = enetsw_readw(priv, ENETSW_MDIOD_REG);
  1667. spin_unlock_bh(&priv->enetsw_mdio_lock);
  1668. return ret;
  1669. }
  1670. static void bcmenet_sw_mdio_write(struct bcm_enet_priv *priv,
  1671. int ext, int phy_id, int location,
  1672. uint16_t data)
  1673. {
  1674. u32 reg;
  1675. spin_lock_bh(&priv->enetsw_mdio_lock);
  1676. enetsw_writel(priv, 0, ENETSW_MDIOC_REG);
  1677. reg = ENETSW_MDIOC_WR_MASK |
  1678. (phy_id << ENETSW_MDIOC_PHYID_SHIFT) |
  1679. (location << ENETSW_MDIOC_REG_SHIFT);
  1680. if (ext)
  1681. reg |= ENETSW_MDIOC_EXT_MASK;
  1682. reg |= data;
  1683. enetsw_writel(priv, reg, ENETSW_MDIOC_REG);
  1684. udelay(50);
  1685. spin_unlock_bh(&priv->enetsw_mdio_lock);
  1686. }
  1687. static inline int bcm_enet_port_is_rgmii(int portid)
  1688. {
  1689. return portid >= ENETSW_RGMII_PORT0;
  1690. }
  1691. /*
  1692. * enet sw PHY polling
  1693. */
  1694. static void swphy_poll_timer(unsigned long data)
  1695. {
  1696. struct bcm_enet_priv *priv = (struct bcm_enet_priv *)data;
  1697. unsigned int i;
  1698. for (i = 0; i < priv->num_ports; i++) {
  1699. struct bcm63xx_enetsw_port *port;
  1700. int val, j, up, advertise, lpa, speed, duplex, media;
  1701. int external_phy = bcm_enet_port_is_rgmii(i);
  1702. u8 override;
  1703. port = &priv->used_ports[i];
  1704. if (!port->used)
  1705. continue;
  1706. if (port->bypass_link)
  1707. continue;
  1708. /* dummy read to clear */
  1709. for (j = 0; j < 2; j++)
  1710. val = bcmenet_sw_mdio_read(priv, external_phy,
  1711. port->phy_id, MII_BMSR);
  1712. if (val == 0xffff)
  1713. continue;
  1714. up = (val & BMSR_LSTATUS) ? 1 : 0;
  1715. if (!(up ^ priv->sw_port_link[i]))
  1716. continue;
  1717. priv->sw_port_link[i] = up;
  1718. /* link changed */
  1719. if (!up) {
  1720. dev_info(&priv->pdev->dev, "link DOWN on %s\n",
  1721. port->name);
  1722. enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
  1723. ENETSW_PORTOV_REG(i));
  1724. enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
  1725. ENETSW_PTCTRL_TXDIS_MASK,
  1726. ENETSW_PTCTRL_REG(i));
  1727. continue;
  1728. }
  1729. advertise = bcmenet_sw_mdio_read(priv, external_phy,
  1730. port->phy_id, MII_ADVERTISE);
  1731. lpa = bcmenet_sw_mdio_read(priv, external_phy, port->phy_id,
  1732. MII_LPA);
  1733. /* figure out media and duplex from advertise and LPA values */
  1734. media = mii_nway_result(lpa & advertise);
  1735. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  1736. if (media & (ADVERTISE_100FULL | ADVERTISE_100HALF))
  1737. speed = 100;
  1738. else
  1739. speed = 10;
  1740. if (val & BMSR_ESTATEN) {
  1741. advertise = bcmenet_sw_mdio_read(priv, external_phy,
  1742. port->phy_id, MII_CTRL1000);
  1743. lpa = bcmenet_sw_mdio_read(priv, external_phy,
  1744. port->phy_id, MII_STAT1000);
  1745. if (advertise & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)
  1746. && lpa & (LPA_1000FULL | LPA_1000HALF)) {
  1747. speed = 1000;
  1748. duplex = (lpa & LPA_1000FULL);
  1749. }
  1750. }
  1751. dev_info(&priv->pdev->dev,
  1752. "link UP on %s, %dMbps, %s-duplex\n",
  1753. port->name, speed, duplex ? "full" : "half");
  1754. override = ENETSW_PORTOV_ENABLE_MASK |
  1755. ENETSW_PORTOV_LINKUP_MASK;
  1756. if (speed == 1000)
  1757. override |= ENETSW_IMPOV_1000_MASK;
  1758. else if (speed == 100)
  1759. override |= ENETSW_IMPOV_100_MASK;
  1760. if (duplex)
  1761. override |= ENETSW_IMPOV_FDX_MASK;
  1762. enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
  1763. enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
  1764. }
  1765. priv->swphy_poll.expires = jiffies + HZ;
  1766. add_timer(&priv->swphy_poll);
  1767. }
  1768. /*
  1769. * open callback, allocate dma rings & buffers and start rx operation
  1770. */
  1771. static int bcm_enetsw_open(struct net_device *dev)
  1772. {
  1773. struct bcm_enet_priv *priv;
  1774. struct device *kdev;
  1775. int i, ret;
  1776. unsigned int size;
  1777. void *p;
  1778. u32 val;
  1779. priv = netdev_priv(dev);
  1780. kdev = &priv->pdev->dev;
  1781. /* mask all interrupts and request them */
  1782. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  1783. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  1784. ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
  1785. 0, dev->name, dev);
  1786. if (ret)
  1787. goto out_freeirq;
  1788. if (priv->irq_tx != -1) {
  1789. ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
  1790. 0, dev->name, dev);
  1791. if (ret)
  1792. goto out_freeirq_rx;
  1793. }
  1794. /* allocate rx dma ring */
  1795. size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
  1796. p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
  1797. if (!p) {
  1798. dev_err(kdev, "cannot allocate rx ring %u\n", size);
  1799. ret = -ENOMEM;
  1800. goto out_freeirq_tx;
  1801. }
  1802. memset(p, 0, size);
  1803. priv->rx_desc_alloc_size = size;
  1804. priv->rx_desc_cpu = p;
  1805. /* allocate tx dma ring */
  1806. size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
  1807. p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
  1808. if (!p) {
  1809. dev_err(kdev, "cannot allocate tx ring\n");
  1810. ret = -ENOMEM;
  1811. goto out_free_rx_ring;
  1812. }
  1813. memset(p, 0, size);
  1814. priv->tx_desc_alloc_size = size;
  1815. priv->tx_desc_cpu = p;
  1816. priv->tx_skb = kzalloc(sizeof(struct sk_buff *) * priv->tx_ring_size,
  1817. GFP_KERNEL);
  1818. if (!priv->tx_skb) {
  1819. dev_err(kdev, "cannot allocate rx skb queue\n");
  1820. ret = -ENOMEM;
  1821. goto out_free_tx_ring;
  1822. }
  1823. priv->tx_desc_count = priv->tx_ring_size;
  1824. priv->tx_dirty_desc = 0;
  1825. priv->tx_curr_desc = 0;
  1826. spin_lock_init(&priv->tx_lock);
  1827. /* init & fill rx ring with skbs */
  1828. priv->rx_skb = kzalloc(sizeof(struct sk_buff *) * priv->rx_ring_size,
  1829. GFP_KERNEL);
  1830. if (!priv->rx_skb) {
  1831. dev_err(kdev, "cannot allocate rx skb queue\n");
  1832. ret = -ENOMEM;
  1833. goto out_free_tx_skb;
  1834. }
  1835. priv->rx_desc_count = 0;
  1836. priv->rx_dirty_desc = 0;
  1837. priv->rx_curr_desc = 0;
  1838. /* disable all ports */
  1839. for (i = 0; i < priv->num_ports; i++) {
  1840. enetsw_writeb(priv, ENETSW_PORTOV_ENABLE_MASK,
  1841. ENETSW_PORTOV_REG(i));
  1842. enetsw_writeb(priv, ENETSW_PTCTRL_RXDIS_MASK |
  1843. ENETSW_PTCTRL_TXDIS_MASK,
  1844. ENETSW_PTCTRL_REG(i));
  1845. priv->sw_port_link[i] = 0;
  1846. }
  1847. /* reset mib */
  1848. val = enetsw_readb(priv, ENETSW_GMCR_REG);
  1849. val |= ENETSW_GMCR_RST_MIB_MASK;
  1850. enetsw_writeb(priv, val, ENETSW_GMCR_REG);
  1851. mdelay(1);
  1852. val &= ~ENETSW_GMCR_RST_MIB_MASK;
  1853. enetsw_writeb(priv, val, ENETSW_GMCR_REG);
  1854. mdelay(1);
  1855. /* force CPU port state */
  1856. val = enetsw_readb(priv, ENETSW_IMPOV_REG);
  1857. val |= ENETSW_IMPOV_FORCE_MASK | ENETSW_IMPOV_LINKUP_MASK;
  1858. enetsw_writeb(priv, val, ENETSW_IMPOV_REG);
  1859. /* enable switch forward engine */
  1860. val = enetsw_readb(priv, ENETSW_SWMODE_REG);
  1861. val |= ENETSW_SWMODE_FWD_EN_MASK;
  1862. enetsw_writeb(priv, val, ENETSW_SWMODE_REG);
  1863. /* enable jumbo on all ports */
  1864. enetsw_writel(priv, 0x1ff, ENETSW_JMBCTL_PORT_REG);
  1865. enetsw_writew(priv, 9728, ENETSW_JMBCTL_MAXSIZE_REG);
  1866. /* initialize flow control buffer allocation */
  1867. enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  1868. ENETDMA_BUFALLOC_REG(priv->rx_chan));
  1869. if (bcm_enet_refill_rx(dev)) {
  1870. dev_err(kdev, "cannot allocate rx skb queue\n");
  1871. ret = -ENOMEM;
  1872. goto out;
  1873. }
  1874. /* write rx & tx ring addresses */
  1875. enet_dmas_writel(priv, priv->rx_desc_dma,
  1876. ENETDMAS_RSTART_REG, priv->rx_chan);
  1877. enet_dmas_writel(priv, priv->tx_desc_dma,
  1878. ENETDMAS_RSTART_REG, priv->tx_chan);
  1879. /* clear remaining state ram for rx & tx channel */
  1880. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->rx_chan);
  1881. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG, priv->tx_chan);
  1882. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->rx_chan);
  1883. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG, priv->tx_chan);
  1884. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->rx_chan);
  1885. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG, priv->tx_chan);
  1886. /* set dma maximum burst len */
  1887. enet_dmac_writel(priv, priv->dma_maxburst,
  1888. ENETDMAC_MAXBURST, priv->rx_chan);
  1889. enet_dmac_writel(priv, priv->dma_maxburst,
  1890. ENETDMAC_MAXBURST, priv->tx_chan);
  1891. /* set flow control low/high threshold to 1/3 / 2/3 */
  1892. val = priv->rx_ring_size / 3;
  1893. enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
  1894. val = (priv->rx_ring_size * 2) / 3;
  1895. enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
  1896. /* all set, enable mac and interrupts, start dma engine and
  1897. * kick rx dma channel
  1898. */
  1899. wmb();
  1900. enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  1901. enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
  1902. ENETDMAC_CHANCFG, priv->rx_chan);
  1903. /* watch "packet transferred" interrupt in rx and tx */
  1904. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1905. ENETDMAC_IR, priv->rx_chan);
  1906. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1907. ENETDMAC_IR, priv->tx_chan);
  1908. /* make sure we enable napi before rx interrupt */
  1909. napi_enable(&priv->napi);
  1910. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1911. ENETDMAC_IRMASK, priv->rx_chan);
  1912. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  1913. ENETDMAC_IRMASK, priv->tx_chan);
  1914. netif_carrier_on(dev);
  1915. netif_start_queue(dev);
  1916. /* apply override config for bypass_link ports here. */
  1917. for (i = 0; i < priv->num_ports; i++) {
  1918. struct bcm63xx_enetsw_port *port;
  1919. u8 override;
  1920. port = &priv->used_ports[i];
  1921. if (!port->used)
  1922. continue;
  1923. if (!port->bypass_link)
  1924. continue;
  1925. override = ENETSW_PORTOV_ENABLE_MASK |
  1926. ENETSW_PORTOV_LINKUP_MASK;
  1927. switch (port->force_speed) {
  1928. case 1000:
  1929. override |= ENETSW_IMPOV_1000_MASK;
  1930. break;
  1931. case 100:
  1932. override |= ENETSW_IMPOV_100_MASK;
  1933. break;
  1934. case 10:
  1935. break;
  1936. default:
  1937. pr_warn("invalid forced speed on port %s: assume 10\n",
  1938. port->name);
  1939. break;
  1940. }
  1941. if (port->force_duplex_full)
  1942. override |= ENETSW_IMPOV_FDX_MASK;
  1943. enetsw_writeb(priv, override, ENETSW_PORTOV_REG(i));
  1944. enetsw_writeb(priv, 0, ENETSW_PTCTRL_REG(i));
  1945. }
  1946. /* start phy polling timer */
  1947. init_timer(&priv->swphy_poll);
  1948. priv->swphy_poll.function = swphy_poll_timer;
  1949. priv->swphy_poll.data = (unsigned long)priv;
  1950. priv->swphy_poll.expires = jiffies;
  1951. add_timer(&priv->swphy_poll);
  1952. return 0;
  1953. out:
  1954. for (i = 0; i < priv->rx_ring_size; i++) {
  1955. struct bcm_enet_desc *desc;
  1956. if (!priv->rx_skb[i])
  1957. continue;
  1958. desc = &priv->rx_desc_cpu[i];
  1959. dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
  1960. DMA_FROM_DEVICE);
  1961. kfree_skb(priv->rx_skb[i]);
  1962. }
  1963. kfree(priv->rx_skb);
  1964. out_free_tx_skb:
  1965. kfree(priv->tx_skb);
  1966. out_free_tx_ring:
  1967. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  1968. priv->tx_desc_cpu, priv->tx_desc_dma);
  1969. out_free_rx_ring:
  1970. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  1971. priv->rx_desc_cpu, priv->rx_desc_dma);
  1972. out_freeirq_tx:
  1973. if (priv->irq_tx != -1)
  1974. free_irq(priv->irq_tx, dev);
  1975. out_freeirq_rx:
  1976. free_irq(priv->irq_rx, dev);
  1977. out_freeirq:
  1978. return ret;
  1979. }
  1980. /* stop callback */
  1981. static int bcm_enetsw_stop(struct net_device *dev)
  1982. {
  1983. struct bcm_enet_priv *priv;
  1984. struct device *kdev;
  1985. int i;
  1986. priv = netdev_priv(dev);
  1987. kdev = &priv->pdev->dev;
  1988. del_timer_sync(&priv->swphy_poll);
  1989. netif_stop_queue(dev);
  1990. napi_disable(&priv->napi);
  1991. del_timer_sync(&priv->rx_timeout);
  1992. /* mask all interrupts */
  1993. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->rx_chan);
  1994. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK, priv->tx_chan);
  1995. /* disable dma & mac */
  1996. bcm_enet_disable_dma(priv, priv->tx_chan);
  1997. bcm_enet_disable_dma(priv, priv->rx_chan);
  1998. /* force reclaim of all tx buffers */
  1999. bcm_enet_tx_reclaim(dev, 1);
  2000. /* free the rx skb ring */
  2001. for (i = 0; i < priv->rx_ring_size; i++) {
  2002. struct bcm_enet_desc *desc;
  2003. if (!priv->rx_skb[i])
  2004. continue;
  2005. desc = &priv->rx_desc_cpu[i];
  2006. dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
  2007. DMA_FROM_DEVICE);
  2008. kfree_skb(priv->rx_skb[i]);
  2009. }
  2010. /* free remaining allocated memory */
  2011. kfree(priv->rx_skb);
  2012. kfree(priv->tx_skb);
  2013. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  2014. priv->rx_desc_cpu, priv->rx_desc_dma);
  2015. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  2016. priv->tx_desc_cpu, priv->tx_desc_dma);
  2017. if (priv->irq_tx != -1)
  2018. free_irq(priv->irq_tx, dev);
  2019. free_irq(priv->irq_rx, dev);
  2020. return 0;
  2021. }
  2022. /* try to sort out phy external status by walking the used_port field
  2023. * in the bcm_enet_priv structure. in case the phy address is not
  2024. * assigned to any physical port on the switch, assume it is external
  2025. * (and yell at the user).
  2026. */
  2027. static int bcm_enetsw_phy_is_external(struct bcm_enet_priv *priv, int phy_id)
  2028. {
  2029. int i;
  2030. for (i = 0; i < priv->num_ports; ++i) {
  2031. if (!priv->used_ports[i].used)
  2032. continue;
  2033. if (priv->used_ports[i].phy_id == phy_id)
  2034. return bcm_enet_port_is_rgmii(i);
  2035. }
  2036. printk_once(KERN_WARNING "bcm63xx_enet: could not find a used port with phy_id %i, assuming phy is external\n",
  2037. phy_id);
  2038. return 1;
  2039. }
  2040. /* can't use bcmenet_sw_mdio_read directly as we need to sort out
  2041. * external/internal status of the given phy_id first.
  2042. */
  2043. static int bcm_enetsw_mii_mdio_read(struct net_device *dev, int phy_id,
  2044. int location)
  2045. {
  2046. struct bcm_enet_priv *priv;
  2047. priv = netdev_priv(dev);
  2048. return bcmenet_sw_mdio_read(priv,
  2049. bcm_enetsw_phy_is_external(priv, phy_id),
  2050. phy_id, location);
  2051. }
  2052. /* can't use bcmenet_sw_mdio_write directly as we need to sort out
  2053. * external/internal status of the given phy_id first.
  2054. */
  2055. static void bcm_enetsw_mii_mdio_write(struct net_device *dev, int phy_id,
  2056. int location,
  2057. int val)
  2058. {
  2059. struct bcm_enet_priv *priv;
  2060. priv = netdev_priv(dev);
  2061. bcmenet_sw_mdio_write(priv, bcm_enetsw_phy_is_external(priv, phy_id),
  2062. phy_id, location, val);
  2063. }
  2064. static int bcm_enetsw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2065. {
  2066. struct mii_if_info mii;
  2067. mii.dev = dev;
  2068. mii.mdio_read = bcm_enetsw_mii_mdio_read;
  2069. mii.mdio_write = bcm_enetsw_mii_mdio_write;
  2070. mii.phy_id = 0;
  2071. mii.phy_id_mask = 0x3f;
  2072. mii.reg_num_mask = 0x1f;
  2073. return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
  2074. }
  2075. static const struct net_device_ops bcm_enetsw_ops = {
  2076. .ndo_open = bcm_enetsw_open,
  2077. .ndo_stop = bcm_enetsw_stop,
  2078. .ndo_start_xmit = bcm_enet_start_xmit,
  2079. .ndo_change_mtu = bcm_enet_change_mtu,
  2080. .ndo_do_ioctl = bcm_enetsw_ioctl,
  2081. };
  2082. static const struct bcm_enet_stats bcm_enetsw_gstrings_stats[] = {
  2083. { "rx_packets", DEV_STAT(rx_packets), -1 },
  2084. { "tx_packets", DEV_STAT(tx_packets), -1 },
  2085. { "rx_bytes", DEV_STAT(rx_bytes), -1 },
  2086. { "tx_bytes", DEV_STAT(tx_bytes), -1 },
  2087. { "rx_errors", DEV_STAT(rx_errors), -1 },
  2088. { "tx_errors", DEV_STAT(tx_errors), -1 },
  2089. { "rx_dropped", DEV_STAT(rx_dropped), -1 },
  2090. { "tx_dropped", DEV_STAT(tx_dropped), -1 },
  2091. { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETHSW_MIB_RX_GD_OCT },
  2092. { "tx_unicast", GEN_STAT(mib.tx_unicast), ETHSW_MIB_RX_BRDCAST },
  2093. { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETHSW_MIB_RX_BRDCAST },
  2094. { "tx_multicast", GEN_STAT(mib.tx_mult), ETHSW_MIB_RX_MULT },
  2095. { "tx_64_octets", GEN_STAT(mib.tx_64), ETHSW_MIB_RX_64 },
  2096. { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETHSW_MIB_RX_65_127 },
  2097. { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETHSW_MIB_RX_128_255 },
  2098. { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETHSW_MIB_RX_256_511 },
  2099. { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETHSW_MIB_RX_512_1023},
  2100. { "tx_1024_1522_oct", GEN_STAT(mib.tx_1024_max),
  2101. ETHSW_MIB_RX_1024_1522 },
  2102. { "tx_1523_2047_oct", GEN_STAT(mib.tx_1523_2047),
  2103. ETHSW_MIB_RX_1523_2047 },
  2104. { "tx_2048_4095_oct", GEN_STAT(mib.tx_2048_4095),
  2105. ETHSW_MIB_RX_2048_4095 },
  2106. { "tx_4096_8191_oct", GEN_STAT(mib.tx_4096_8191),
  2107. ETHSW_MIB_RX_4096_8191 },
  2108. { "tx_8192_9728_oct", GEN_STAT(mib.tx_8192_9728),
  2109. ETHSW_MIB_RX_8192_9728 },
  2110. { "tx_oversize", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR },
  2111. { "tx_oversize_drop", GEN_STAT(mib.tx_ovr), ETHSW_MIB_RX_OVR_DISC },
  2112. { "tx_dropped", GEN_STAT(mib.tx_drop), ETHSW_MIB_RX_DROP },
  2113. { "tx_undersize", GEN_STAT(mib.tx_underrun), ETHSW_MIB_RX_UND },
  2114. { "tx_pause", GEN_STAT(mib.tx_pause), ETHSW_MIB_RX_PAUSE },
  2115. { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETHSW_MIB_TX_ALL_OCT },
  2116. { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETHSW_MIB_TX_BRDCAST },
  2117. { "rx_multicast", GEN_STAT(mib.rx_mult), ETHSW_MIB_TX_MULT },
  2118. { "rx_unicast", GEN_STAT(mib.rx_unicast), ETHSW_MIB_TX_MULT },
  2119. { "rx_pause", GEN_STAT(mib.rx_pause), ETHSW_MIB_TX_PAUSE },
  2120. { "rx_dropped", GEN_STAT(mib.rx_drop), ETHSW_MIB_TX_DROP_PKTS },
  2121. };
  2122. #define BCM_ENETSW_STATS_LEN \
  2123. (sizeof(bcm_enetsw_gstrings_stats) / sizeof(struct bcm_enet_stats))
  2124. static void bcm_enetsw_get_strings(struct net_device *netdev,
  2125. u32 stringset, u8 *data)
  2126. {
  2127. int i;
  2128. switch (stringset) {
  2129. case ETH_SS_STATS:
  2130. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2131. memcpy(data + i * ETH_GSTRING_LEN,
  2132. bcm_enetsw_gstrings_stats[i].stat_string,
  2133. ETH_GSTRING_LEN);
  2134. }
  2135. break;
  2136. }
  2137. }
  2138. static int bcm_enetsw_get_sset_count(struct net_device *netdev,
  2139. int string_set)
  2140. {
  2141. switch (string_set) {
  2142. case ETH_SS_STATS:
  2143. return BCM_ENETSW_STATS_LEN;
  2144. default:
  2145. return -EINVAL;
  2146. }
  2147. }
  2148. static void bcm_enetsw_get_drvinfo(struct net_device *netdev,
  2149. struct ethtool_drvinfo *drvinfo)
  2150. {
  2151. strncpy(drvinfo->driver, bcm_enet_driver_name, 32);
  2152. strncpy(drvinfo->version, bcm_enet_driver_version, 32);
  2153. strncpy(drvinfo->fw_version, "N/A", 32);
  2154. strncpy(drvinfo->bus_info, "bcm63xx", 32);
  2155. }
  2156. static void bcm_enetsw_get_ethtool_stats(struct net_device *netdev,
  2157. struct ethtool_stats *stats,
  2158. u64 *data)
  2159. {
  2160. struct bcm_enet_priv *priv;
  2161. int i;
  2162. priv = netdev_priv(netdev);
  2163. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2164. const struct bcm_enet_stats *s;
  2165. u32 lo, hi;
  2166. char *p;
  2167. int reg;
  2168. s = &bcm_enetsw_gstrings_stats[i];
  2169. reg = s->mib_reg;
  2170. if (reg == -1)
  2171. continue;
  2172. lo = enetsw_readl(priv, ENETSW_MIB_REG(reg));
  2173. p = (char *)priv + s->stat_offset;
  2174. if (s->sizeof_stat == sizeof(u64)) {
  2175. hi = enetsw_readl(priv, ENETSW_MIB_REG(reg + 1));
  2176. *(u64 *)p = ((u64)hi << 32 | lo);
  2177. } else {
  2178. *(u32 *)p = lo;
  2179. }
  2180. }
  2181. for (i = 0; i < BCM_ENETSW_STATS_LEN; i++) {
  2182. const struct bcm_enet_stats *s;
  2183. char *p;
  2184. s = &bcm_enetsw_gstrings_stats[i];
  2185. if (s->mib_reg == -1)
  2186. p = (char *)&netdev->stats + s->stat_offset;
  2187. else
  2188. p = (char *)priv + s->stat_offset;
  2189. data[i] = (s->sizeof_stat == sizeof(u64)) ?
  2190. *(u64 *)p : *(u32 *)p;
  2191. }
  2192. }
  2193. static void bcm_enetsw_get_ringparam(struct net_device *dev,
  2194. struct ethtool_ringparam *ering)
  2195. {
  2196. struct bcm_enet_priv *priv;
  2197. priv = netdev_priv(dev);
  2198. /* rx/tx ring is actually only limited by memory */
  2199. ering->rx_max_pending = 8192;
  2200. ering->tx_max_pending = 8192;
  2201. ering->rx_mini_max_pending = 0;
  2202. ering->rx_jumbo_max_pending = 0;
  2203. ering->rx_pending = priv->rx_ring_size;
  2204. ering->tx_pending = priv->tx_ring_size;
  2205. }
  2206. static int bcm_enetsw_set_ringparam(struct net_device *dev,
  2207. struct ethtool_ringparam *ering)
  2208. {
  2209. struct bcm_enet_priv *priv;
  2210. int was_running;
  2211. priv = netdev_priv(dev);
  2212. was_running = 0;
  2213. if (netif_running(dev)) {
  2214. bcm_enetsw_stop(dev);
  2215. was_running = 1;
  2216. }
  2217. priv->rx_ring_size = ering->rx_pending;
  2218. priv->tx_ring_size = ering->tx_pending;
  2219. if (was_running) {
  2220. int err;
  2221. err = bcm_enetsw_open(dev);
  2222. if (err)
  2223. dev_close(dev);
  2224. }
  2225. return 0;
  2226. }
  2227. static struct ethtool_ops bcm_enetsw_ethtool_ops = {
  2228. .get_strings = bcm_enetsw_get_strings,
  2229. .get_sset_count = bcm_enetsw_get_sset_count,
  2230. .get_ethtool_stats = bcm_enetsw_get_ethtool_stats,
  2231. .get_drvinfo = bcm_enetsw_get_drvinfo,
  2232. .get_ringparam = bcm_enetsw_get_ringparam,
  2233. .set_ringparam = bcm_enetsw_set_ringparam,
  2234. };
  2235. /* allocate netdevice, request register memory and register device. */
  2236. static int bcm_enetsw_probe(struct platform_device *pdev)
  2237. {
  2238. struct bcm_enet_priv *priv;
  2239. struct net_device *dev;
  2240. struct bcm63xx_enetsw_platform_data *pd;
  2241. struct resource *res_mem;
  2242. int ret, irq_rx, irq_tx;
  2243. /* stop if shared driver failed, assume driver->probe will be
  2244. * called in the same order we register devices (correct ?)
  2245. */
  2246. if (!bcm_enet_shared_base[0])
  2247. return -ENODEV;
  2248. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2249. irq_rx = platform_get_irq(pdev, 0);
  2250. irq_tx = platform_get_irq(pdev, 1);
  2251. if (!res_mem || irq_rx < 0)
  2252. return -ENODEV;
  2253. ret = 0;
  2254. dev = alloc_etherdev(sizeof(*priv));
  2255. if (!dev)
  2256. return -ENOMEM;
  2257. priv = netdev_priv(dev);
  2258. memset(priv, 0, sizeof(*priv));
  2259. /* initialize default and fetch platform data */
  2260. priv->enet_is_sw = true;
  2261. priv->irq_rx = irq_rx;
  2262. priv->irq_tx = irq_tx;
  2263. priv->rx_ring_size = BCMENET_DEF_RX_DESC;
  2264. priv->tx_ring_size = BCMENET_DEF_TX_DESC;
  2265. priv->dma_maxburst = BCMENETSW_DMA_MAXBURST;
  2266. pd = dev_get_platdata(&pdev->dev);
  2267. if (pd) {
  2268. memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
  2269. memcpy(priv->used_ports, pd->used_ports,
  2270. sizeof(pd->used_ports));
  2271. priv->num_ports = pd->num_ports;
  2272. priv->dma_has_sram = pd->dma_has_sram;
  2273. priv->dma_chan_en_mask = pd->dma_chan_en_mask;
  2274. priv->dma_chan_int_mask = pd->dma_chan_int_mask;
  2275. priv->dma_chan_width = pd->dma_chan_width;
  2276. }
  2277. ret = compute_hw_mtu(priv, dev->mtu);
  2278. if (ret)
  2279. goto out;
  2280. if (!request_mem_region(res_mem->start, resource_size(res_mem),
  2281. "bcm63xx_enetsw")) {
  2282. ret = -EBUSY;
  2283. goto out;
  2284. }
  2285. priv->base = ioremap(res_mem->start, resource_size(res_mem));
  2286. if (priv->base == NULL) {
  2287. ret = -ENOMEM;
  2288. goto out_release_mem;
  2289. }
  2290. priv->mac_clk = clk_get(&pdev->dev, "enetsw");
  2291. if (IS_ERR(priv->mac_clk)) {
  2292. ret = PTR_ERR(priv->mac_clk);
  2293. goto out_unmap;
  2294. }
  2295. ret = clk_prepare_enable(priv->mac_clk);
  2296. if (ret)
  2297. goto out_put_clk;
  2298. priv->rx_chan = 0;
  2299. priv->tx_chan = 1;
  2300. spin_lock_init(&priv->rx_lock);
  2301. /* init rx timeout (used for oom) */
  2302. init_timer(&priv->rx_timeout);
  2303. priv->rx_timeout.function = bcm_enet_refill_rx_timer;
  2304. priv->rx_timeout.data = (unsigned long)dev;
  2305. /* register netdevice */
  2306. dev->netdev_ops = &bcm_enetsw_ops;
  2307. netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
  2308. dev->ethtool_ops = &bcm_enetsw_ethtool_ops;
  2309. SET_NETDEV_DEV(dev, &pdev->dev);
  2310. spin_lock_init(&priv->enetsw_mdio_lock);
  2311. ret = register_netdev(dev);
  2312. if (ret)
  2313. goto out_disable_clk;
  2314. netif_carrier_off(dev);
  2315. platform_set_drvdata(pdev, dev);
  2316. priv->pdev = pdev;
  2317. priv->net_dev = dev;
  2318. return 0;
  2319. out_disable_clk:
  2320. clk_disable_unprepare(priv->mac_clk);
  2321. out_put_clk:
  2322. clk_put(priv->mac_clk);
  2323. out_unmap:
  2324. iounmap(priv->base);
  2325. out_release_mem:
  2326. release_mem_region(res_mem->start, resource_size(res_mem));
  2327. out:
  2328. free_netdev(dev);
  2329. return ret;
  2330. }
  2331. /* exit func, stops hardware and unregisters netdevice */
  2332. static int bcm_enetsw_remove(struct platform_device *pdev)
  2333. {
  2334. struct bcm_enet_priv *priv;
  2335. struct net_device *dev;
  2336. struct resource *res;
  2337. /* stop netdevice */
  2338. dev = platform_get_drvdata(pdev);
  2339. priv = netdev_priv(dev);
  2340. unregister_netdev(dev);
  2341. /* release device resources */
  2342. iounmap(priv->base);
  2343. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2344. release_mem_region(res->start, resource_size(res));
  2345. clk_disable_unprepare(priv->mac_clk);
  2346. clk_put(priv->mac_clk);
  2347. free_netdev(dev);
  2348. return 0;
  2349. }
  2350. struct platform_driver bcm63xx_enetsw_driver = {
  2351. .probe = bcm_enetsw_probe,
  2352. .remove = bcm_enetsw_remove,
  2353. .driver = {
  2354. .name = "bcm63xx_enetsw",
  2355. .owner = THIS_MODULE,
  2356. },
  2357. };
  2358. /* reserve & remap memory space shared between all macs */
  2359. static int bcm_enet_shared_probe(struct platform_device *pdev)
  2360. {
  2361. struct resource *res;
  2362. void __iomem *p[3];
  2363. unsigned int i;
  2364. memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
  2365. for (i = 0; i < 3; i++) {
  2366. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  2367. p[i] = devm_ioremap_resource(&pdev->dev, res);
  2368. if (IS_ERR(p[i]))
  2369. return PTR_ERR(p[i]);
  2370. }
  2371. memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
  2372. return 0;
  2373. }
  2374. static int bcm_enet_shared_remove(struct platform_device *pdev)
  2375. {
  2376. return 0;
  2377. }
  2378. /* this "shared" driver is needed because both macs share a single
  2379. * address space
  2380. */
  2381. struct platform_driver bcm63xx_enet_shared_driver = {
  2382. .probe = bcm_enet_shared_probe,
  2383. .remove = bcm_enet_shared_remove,
  2384. .driver = {
  2385. .name = "bcm63xx_enet_shared",
  2386. .owner = THIS_MODULE,
  2387. },
  2388. };
  2389. /* entry point */
  2390. static int __init bcm_enet_init(void)
  2391. {
  2392. int ret;
  2393. ret = platform_driver_register(&bcm63xx_enet_shared_driver);
  2394. if (ret)
  2395. return ret;
  2396. ret = platform_driver_register(&bcm63xx_enet_driver);
  2397. if (ret)
  2398. platform_driver_unregister(&bcm63xx_enet_shared_driver);
  2399. ret = platform_driver_register(&bcm63xx_enetsw_driver);
  2400. if (ret) {
  2401. platform_driver_unregister(&bcm63xx_enet_driver);
  2402. platform_driver_unregister(&bcm63xx_enet_shared_driver);
  2403. }
  2404. return ret;
  2405. }
  2406. static void __exit bcm_enet_exit(void)
  2407. {
  2408. platform_driver_unregister(&bcm63xx_enet_driver);
  2409. platform_driver_unregister(&bcm63xx_enetsw_driver);
  2410. platform_driver_unregister(&bcm63xx_enet_shared_driver);
  2411. }
  2412. module_init(bcm_enet_init);
  2413. module_exit(bcm_enet_exit);
  2414. MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
  2415. MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
  2416. MODULE_LICENSE("GPL");