bcm63xx_enet.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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 shared registers
  56. */
  57. static inline u32 enet_dma_readl(struct bcm_enet_priv *priv, u32 off)
  58. {
  59. return bcm_readl(bcm_enet_shared_base[0] + off);
  60. }
  61. static inline void enet_dma_writel(struct bcm_enet_priv *priv,
  62. u32 val, u32 off)
  63. {
  64. bcm_writel(val, bcm_enet_shared_base[0] + off);
  65. }
  66. static inline u32 enet_dmac_readl(struct bcm_enet_priv *priv, u32 off)
  67. {
  68. return bcm_readl(bcm_enet_shared_base[1] + off);
  69. }
  70. static inline void enet_dmac_writel(struct bcm_enet_priv *priv,
  71. u32 val, u32 off)
  72. {
  73. bcm_writel(val, bcm_enet_shared_base[1] + off);
  74. }
  75. static inline u32 enet_dmas_readl(struct bcm_enet_priv *priv, u32 off)
  76. {
  77. return bcm_readl(bcm_enet_shared_base[2] + off);
  78. }
  79. static inline void enet_dmas_writel(struct bcm_enet_priv *priv,
  80. u32 val, u32 off)
  81. {
  82. bcm_writel(val, bcm_enet_shared_base[2] + off);
  83. }
  84. /*
  85. * write given data into mii register and wait for transfer to end
  86. * with timeout (average measured transfer time is 25us)
  87. */
  88. static int do_mdio_op(struct bcm_enet_priv *priv, unsigned int data)
  89. {
  90. int limit;
  91. /* make sure mii interrupt status is cleared */
  92. enet_writel(priv, ENET_IR_MII, ENET_IR_REG);
  93. enet_writel(priv, data, ENET_MIIDATA_REG);
  94. wmb();
  95. /* busy wait on mii interrupt bit, with timeout */
  96. limit = 1000;
  97. do {
  98. if (enet_readl(priv, ENET_IR_REG) & ENET_IR_MII)
  99. break;
  100. udelay(1);
  101. } while (limit-- > 0);
  102. return (limit < 0) ? 1 : 0;
  103. }
  104. /*
  105. * MII internal read callback
  106. */
  107. static int bcm_enet_mdio_read(struct bcm_enet_priv *priv, int mii_id,
  108. int regnum)
  109. {
  110. u32 tmp, val;
  111. tmp = regnum << ENET_MIIDATA_REG_SHIFT;
  112. tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
  113. tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
  114. tmp |= ENET_MIIDATA_OP_READ_MASK;
  115. if (do_mdio_op(priv, tmp))
  116. return -1;
  117. val = enet_readl(priv, ENET_MIIDATA_REG);
  118. val &= 0xffff;
  119. return val;
  120. }
  121. /*
  122. * MII internal write callback
  123. */
  124. static int bcm_enet_mdio_write(struct bcm_enet_priv *priv, int mii_id,
  125. int regnum, u16 value)
  126. {
  127. u32 tmp;
  128. tmp = (value & 0xffff) << ENET_MIIDATA_DATA_SHIFT;
  129. tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
  130. tmp |= regnum << ENET_MIIDATA_REG_SHIFT;
  131. tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
  132. tmp |= ENET_MIIDATA_OP_WRITE_MASK;
  133. (void)do_mdio_op(priv, tmp);
  134. return 0;
  135. }
  136. /*
  137. * MII read callback from phylib
  138. */
  139. static int bcm_enet_mdio_read_phylib(struct mii_bus *bus, int mii_id,
  140. int regnum)
  141. {
  142. return bcm_enet_mdio_read(bus->priv, mii_id, regnum);
  143. }
  144. /*
  145. * MII write callback from phylib
  146. */
  147. static int bcm_enet_mdio_write_phylib(struct mii_bus *bus, int mii_id,
  148. int regnum, u16 value)
  149. {
  150. return bcm_enet_mdio_write(bus->priv, mii_id, regnum, value);
  151. }
  152. /*
  153. * MII read callback from mii core
  154. */
  155. static int bcm_enet_mdio_read_mii(struct net_device *dev, int mii_id,
  156. int regnum)
  157. {
  158. return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
  159. }
  160. /*
  161. * MII write callback from mii core
  162. */
  163. static void bcm_enet_mdio_write_mii(struct net_device *dev, int mii_id,
  164. int regnum, int value)
  165. {
  166. bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
  167. }
  168. /*
  169. * refill rx queue
  170. */
  171. static int bcm_enet_refill_rx(struct net_device *dev)
  172. {
  173. struct bcm_enet_priv *priv;
  174. priv = netdev_priv(dev);
  175. while (priv->rx_desc_count < priv->rx_ring_size) {
  176. struct bcm_enet_desc *desc;
  177. struct sk_buff *skb;
  178. dma_addr_t p;
  179. int desc_idx;
  180. u32 len_stat;
  181. desc_idx = priv->rx_dirty_desc;
  182. desc = &priv->rx_desc_cpu[desc_idx];
  183. if (!priv->rx_skb[desc_idx]) {
  184. skb = netdev_alloc_skb(dev, priv->rx_skb_size);
  185. if (!skb)
  186. break;
  187. priv->rx_skb[desc_idx] = skb;
  188. p = dma_map_single(&priv->pdev->dev, skb->data,
  189. priv->rx_skb_size,
  190. DMA_FROM_DEVICE);
  191. desc->address = p;
  192. }
  193. len_stat = priv->rx_skb_size << DMADESC_LENGTH_SHIFT;
  194. len_stat |= DMADESC_OWNER_MASK;
  195. if (priv->rx_dirty_desc == priv->rx_ring_size - 1) {
  196. len_stat |= DMADESC_WRAP_MASK;
  197. priv->rx_dirty_desc = 0;
  198. } else {
  199. priv->rx_dirty_desc++;
  200. }
  201. wmb();
  202. desc->len_stat = len_stat;
  203. priv->rx_desc_count++;
  204. /* tell dma engine we allocated one buffer */
  205. enet_dma_writel(priv, 1, ENETDMA_BUFALLOC_REG(priv->rx_chan));
  206. }
  207. /* If rx ring is still empty, set a timer to try allocating
  208. * again at a later time. */
  209. if (priv->rx_desc_count == 0 && netif_running(dev)) {
  210. dev_warn(&priv->pdev->dev, "unable to refill rx ring\n");
  211. priv->rx_timeout.expires = jiffies + HZ;
  212. add_timer(&priv->rx_timeout);
  213. }
  214. return 0;
  215. }
  216. /*
  217. * timer callback to defer refill rx queue in case we're OOM
  218. */
  219. static void bcm_enet_refill_rx_timer(unsigned long data)
  220. {
  221. struct net_device *dev;
  222. struct bcm_enet_priv *priv;
  223. dev = (struct net_device *)data;
  224. priv = netdev_priv(dev);
  225. spin_lock(&priv->rx_lock);
  226. bcm_enet_refill_rx((struct net_device *)data);
  227. spin_unlock(&priv->rx_lock);
  228. }
  229. /*
  230. * extract packet from rx queue
  231. */
  232. static int bcm_enet_receive_queue(struct net_device *dev, int budget)
  233. {
  234. struct bcm_enet_priv *priv;
  235. struct device *kdev;
  236. int processed;
  237. priv = netdev_priv(dev);
  238. kdev = &priv->pdev->dev;
  239. processed = 0;
  240. /* don't scan ring further than number of refilled
  241. * descriptor */
  242. if (budget > priv->rx_desc_count)
  243. budget = priv->rx_desc_count;
  244. do {
  245. struct bcm_enet_desc *desc;
  246. struct sk_buff *skb;
  247. int desc_idx;
  248. u32 len_stat;
  249. unsigned int len;
  250. desc_idx = priv->rx_curr_desc;
  251. desc = &priv->rx_desc_cpu[desc_idx];
  252. /* make sure we actually read the descriptor status at
  253. * each loop */
  254. rmb();
  255. len_stat = desc->len_stat;
  256. /* break if dma ownership belongs to hw */
  257. if (len_stat & DMADESC_OWNER_MASK)
  258. break;
  259. processed++;
  260. priv->rx_curr_desc++;
  261. if (priv->rx_curr_desc == priv->rx_ring_size)
  262. priv->rx_curr_desc = 0;
  263. priv->rx_desc_count--;
  264. /* if the packet does not have start of packet _and_
  265. * end of packet flag set, then just recycle it */
  266. if ((len_stat & DMADESC_ESOP_MASK) != DMADESC_ESOP_MASK) {
  267. dev->stats.rx_dropped++;
  268. continue;
  269. }
  270. /* recycle packet if it's marked as bad */
  271. if (unlikely(len_stat & DMADESC_ERR_MASK)) {
  272. dev->stats.rx_errors++;
  273. if (len_stat & DMADESC_OVSIZE_MASK)
  274. dev->stats.rx_length_errors++;
  275. if (len_stat & DMADESC_CRC_MASK)
  276. dev->stats.rx_crc_errors++;
  277. if (len_stat & DMADESC_UNDER_MASK)
  278. dev->stats.rx_frame_errors++;
  279. if (len_stat & DMADESC_OV_MASK)
  280. dev->stats.rx_fifo_errors++;
  281. continue;
  282. }
  283. /* valid packet */
  284. skb = priv->rx_skb[desc_idx];
  285. len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
  286. /* don't include FCS */
  287. len -= 4;
  288. if (len < copybreak) {
  289. struct sk_buff *nskb;
  290. nskb = netdev_alloc_skb_ip_align(dev, len);
  291. if (!nskb) {
  292. /* forget packet, just rearm desc */
  293. dev->stats.rx_dropped++;
  294. continue;
  295. }
  296. dma_sync_single_for_cpu(kdev, desc->address,
  297. len, DMA_FROM_DEVICE);
  298. memcpy(nskb->data, skb->data, len);
  299. dma_sync_single_for_device(kdev, desc->address,
  300. len, DMA_FROM_DEVICE);
  301. skb = nskb;
  302. } else {
  303. dma_unmap_single(&priv->pdev->dev, desc->address,
  304. priv->rx_skb_size, DMA_FROM_DEVICE);
  305. priv->rx_skb[desc_idx] = NULL;
  306. }
  307. skb_put(skb, len);
  308. skb->protocol = eth_type_trans(skb, dev);
  309. dev->stats.rx_packets++;
  310. dev->stats.rx_bytes += len;
  311. netif_receive_skb(skb);
  312. } while (--budget > 0);
  313. if (processed || !priv->rx_desc_count) {
  314. bcm_enet_refill_rx(dev);
  315. /* kick rx dma */
  316. enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
  317. ENETDMAC_CHANCFG_REG(priv->rx_chan));
  318. }
  319. return processed;
  320. }
  321. /*
  322. * try to or force reclaim of transmitted buffers
  323. */
  324. static int bcm_enet_tx_reclaim(struct net_device *dev, int force)
  325. {
  326. struct bcm_enet_priv *priv;
  327. int released;
  328. priv = netdev_priv(dev);
  329. released = 0;
  330. while (priv->tx_desc_count < priv->tx_ring_size) {
  331. struct bcm_enet_desc *desc;
  332. struct sk_buff *skb;
  333. /* We run in a bh and fight against start_xmit, which
  334. * is called with bh disabled */
  335. spin_lock(&priv->tx_lock);
  336. desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
  337. if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
  338. spin_unlock(&priv->tx_lock);
  339. break;
  340. }
  341. /* ensure other field of the descriptor were not read
  342. * before we checked ownership */
  343. rmb();
  344. skb = priv->tx_skb[priv->tx_dirty_desc];
  345. priv->tx_skb[priv->tx_dirty_desc] = NULL;
  346. dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
  347. DMA_TO_DEVICE);
  348. priv->tx_dirty_desc++;
  349. if (priv->tx_dirty_desc == priv->tx_ring_size)
  350. priv->tx_dirty_desc = 0;
  351. priv->tx_desc_count++;
  352. spin_unlock(&priv->tx_lock);
  353. if (desc->len_stat & DMADESC_UNDER_MASK)
  354. dev->stats.tx_errors++;
  355. dev_kfree_skb(skb);
  356. released++;
  357. }
  358. if (netif_queue_stopped(dev) && released)
  359. netif_wake_queue(dev);
  360. return released;
  361. }
  362. /*
  363. * poll func, called by network core
  364. */
  365. static int bcm_enet_poll(struct napi_struct *napi, int budget)
  366. {
  367. struct bcm_enet_priv *priv;
  368. struct net_device *dev;
  369. int tx_work_done, rx_work_done;
  370. priv = container_of(napi, struct bcm_enet_priv, napi);
  371. dev = priv->net_dev;
  372. /* ack interrupts */
  373. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  374. ENETDMAC_IR_REG(priv->rx_chan));
  375. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  376. ENETDMAC_IR_REG(priv->tx_chan));
  377. /* reclaim sent skb */
  378. tx_work_done = bcm_enet_tx_reclaim(dev, 0);
  379. spin_lock(&priv->rx_lock);
  380. rx_work_done = bcm_enet_receive_queue(dev, budget);
  381. spin_unlock(&priv->rx_lock);
  382. if (rx_work_done >= budget || tx_work_done > 0) {
  383. /* rx/tx queue is not yet empty/clean */
  384. return rx_work_done;
  385. }
  386. /* no more packet in rx/tx queue, remove device from poll
  387. * queue */
  388. napi_complete(napi);
  389. /* restore rx/tx interrupt */
  390. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  391. ENETDMAC_IRMASK_REG(priv->rx_chan));
  392. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  393. ENETDMAC_IRMASK_REG(priv->tx_chan));
  394. return rx_work_done;
  395. }
  396. /*
  397. * mac interrupt handler
  398. */
  399. static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
  400. {
  401. struct net_device *dev;
  402. struct bcm_enet_priv *priv;
  403. u32 stat;
  404. dev = dev_id;
  405. priv = netdev_priv(dev);
  406. stat = enet_readl(priv, ENET_IR_REG);
  407. if (!(stat & ENET_IR_MIB))
  408. return IRQ_NONE;
  409. /* clear & mask interrupt */
  410. enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
  411. enet_writel(priv, 0, ENET_IRMASK_REG);
  412. /* read mib registers in workqueue */
  413. schedule_work(&priv->mib_update_task);
  414. return IRQ_HANDLED;
  415. }
  416. /*
  417. * rx/tx dma interrupt handler
  418. */
  419. static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
  420. {
  421. struct net_device *dev;
  422. struct bcm_enet_priv *priv;
  423. dev = dev_id;
  424. priv = netdev_priv(dev);
  425. /* mask rx/tx interrupts */
  426. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->rx_chan));
  427. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->tx_chan));
  428. napi_schedule(&priv->napi);
  429. return IRQ_HANDLED;
  430. }
  431. /*
  432. * tx request callback
  433. */
  434. static int bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  435. {
  436. struct bcm_enet_priv *priv;
  437. struct bcm_enet_desc *desc;
  438. u32 len_stat;
  439. int ret;
  440. priv = netdev_priv(dev);
  441. /* lock against tx reclaim */
  442. spin_lock(&priv->tx_lock);
  443. /* make sure the tx hw queue is not full, should not happen
  444. * since we stop queue before it's the case */
  445. if (unlikely(!priv->tx_desc_count)) {
  446. netif_stop_queue(dev);
  447. dev_err(&priv->pdev->dev, "xmit called with no tx desc "
  448. "available?\n");
  449. ret = NETDEV_TX_BUSY;
  450. goto out_unlock;
  451. }
  452. /* point to the next available desc */
  453. desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
  454. priv->tx_skb[priv->tx_curr_desc] = skb;
  455. /* fill descriptor */
  456. desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
  457. DMA_TO_DEVICE);
  458. len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
  459. len_stat |= DMADESC_ESOP_MASK |
  460. DMADESC_APPEND_CRC |
  461. DMADESC_OWNER_MASK;
  462. priv->tx_curr_desc++;
  463. if (priv->tx_curr_desc == priv->tx_ring_size) {
  464. priv->tx_curr_desc = 0;
  465. len_stat |= DMADESC_WRAP_MASK;
  466. }
  467. priv->tx_desc_count--;
  468. /* dma might be already polling, make sure we update desc
  469. * fields in correct order */
  470. wmb();
  471. desc->len_stat = len_stat;
  472. wmb();
  473. /* kick tx dma */
  474. enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
  475. ENETDMAC_CHANCFG_REG(priv->tx_chan));
  476. /* stop queue if no more desc available */
  477. if (!priv->tx_desc_count)
  478. netif_stop_queue(dev);
  479. dev->stats.tx_bytes += skb->len;
  480. dev->stats.tx_packets++;
  481. ret = NETDEV_TX_OK;
  482. out_unlock:
  483. spin_unlock(&priv->tx_lock);
  484. return ret;
  485. }
  486. /*
  487. * Change the interface's mac address.
  488. */
  489. static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
  490. {
  491. struct bcm_enet_priv *priv;
  492. struct sockaddr *addr = p;
  493. u32 val;
  494. priv = netdev_priv(dev);
  495. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  496. /* use perfect match register 0 to store my mac address */
  497. val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
  498. (dev->dev_addr[4] << 8) | dev->dev_addr[5];
  499. enet_writel(priv, val, ENET_PML_REG(0));
  500. val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  501. val |= ENET_PMH_DATAVALID_MASK;
  502. enet_writel(priv, val, ENET_PMH_REG(0));
  503. return 0;
  504. }
  505. /*
  506. * Change rx mode (promiscuous/allmulti) and update multicast list
  507. */
  508. static void bcm_enet_set_multicast_list(struct net_device *dev)
  509. {
  510. struct bcm_enet_priv *priv;
  511. struct netdev_hw_addr *ha;
  512. u32 val;
  513. int i;
  514. priv = netdev_priv(dev);
  515. val = enet_readl(priv, ENET_RXCFG_REG);
  516. if (dev->flags & IFF_PROMISC)
  517. val |= ENET_RXCFG_PROMISC_MASK;
  518. else
  519. val &= ~ENET_RXCFG_PROMISC_MASK;
  520. /* only 3 perfect match registers left, first one is used for
  521. * own mac address */
  522. if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
  523. val |= ENET_RXCFG_ALLMCAST_MASK;
  524. else
  525. val &= ~ENET_RXCFG_ALLMCAST_MASK;
  526. /* no need to set perfect match registers if we catch all
  527. * multicast */
  528. if (val & ENET_RXCFG_ALLMCAST_MASK) {
  529. enet_writel(priv, val, ENET_RXCFG_REG);
  530. return;
  531. }
  532. i = 0;
  533. netdev_for_each_mc_addr(ha, dev) {
  534. u8 *dmi_addr;
  535. u32 tmp;
  536. if (i == 3)
  537. break;
  538. /* update perfect match registers */
  539. dmi_addr = ha->addr;
  540. tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
  541. (dmi_addr[4] << 8) | dmi_addr[5];
  542. enet_writel(priv, tmp, ENET_PML_REG(i + 1));
  543. tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
  544. tmp |= ENET_PMH_DATAVALID_MASK;
  545. enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
  546. }
  547. for (; i < 3; i++) {
  548. enet_writel(priv, 0, ENET_PML_REG(i + 1));
  549. enet_writel(priv, 0, ENET_PMH_REG(i + 1));
  550. }
  551. enet_writel(priv, val, ENET_RXCFG_REG);
  552. }
  553. /*
  554. * set mac duplex parameters
  555. */
  556. static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
  557. {
  558. u32 val;
  559. val = enet_readl(priv, ENET_TXCTL_REG);
  560. if (fullduplex)
  561. val |= ENET_TXCTL_FD_MASK;
  562. else
  563. val &= ~ENET_TXCTL_FD_MASK;
  564. enet_writel(priv, val, ENET_TXCTL_REG);
  565. }
  566. /*
  567. * set mac flow control parameters
  568. */
  569. static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
  570. {
  571. u32 val;
  572. /* rx flow control (pause frame handling) */
  573. val = enet_readl(priv, ENET_RXCFG_REG);
  574. if (rx_en)
  575. val |= ENET_RXCFG_ENFLOW_MASK;
  576. else
  577. val &= ~ENET_RXCFG_ENFLOW_MASK;
  578. enet_writel(priv, val, ENET_RXCFG_REG);
  579. /* tx flow control (pause frame generation) */
  580. val = enet_dma_readl(priv, ENETDMA_CFG_REG);
  581. if (tx_en)
  582. val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
  583. else
  584. val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
  585. enet_dma_writel(priv, val, ENETDMA_CFG_REG);
  586. }
  587. /*
  588. * link changed callback (from phylib)
  589. */
  590. static void bcm_enet_adjust_phy_link(struct net_device *dev)
  591. {
  592. struct bcm_enet_priv *priv;
  593. struct phy_device *phydev;
  594. int status_changed;
  595. priv = netdev_priv(dev);
  596. phydev = priv->phydev;
  597. status_changed = 0;
  598. if (priv->old_link != phydev->link) {
  599. status_changed = 1;
  600. priv->old_link = phydev->link;
  601. }
  602. /* reflect duplex change in mac configuration */
  603. if (phydev->link && phydev->duplex != priv->old_duplex) {
  604. bcm_enet_set_duplex(priv,
  605. (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
  606. status_changed = 1;
  607. priv->old_duplex = phydev->duplex;
  608. }
  609. /* enable flow control if remote advertise it (trust phylib to
  610. * check that duplex is full */
  611. if (phydev->link && phydev->pause != priv->old_pause) {
  612. int rx_pause_en, tx_pause_en;
  613. if (phydev->pause) {
  614. /* pause was advertised by lpa and us */
  615. rx_pause_en = 1;
  616. tx_pause_en = 1;
  617. } else if (!priv->pause_auto) {
  618. /* pause setting overrided by user */
  619. rx_pause_en = priv->pause_rx;
  620. tx_pause_en = priv->pause_tx;
  621. } else {
  622. rx_pause_en = 0;
  623. tx_pause_en = 0;
  624. }
  625. bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
  626. status_changed = 1;
  627. priv->old_pause = phydev->pause;
  628. }
  629. if (status_changed) {
  630. pr_info("%s: link %s", dev->name, phydev->link ?
  631. "UP" : "DOWN");
  632. if (phydev->link)
  633. pr_cont(" - %d/%s - flow control %s", phydev->speed,
  634. DUPLEX_FULL == phydev->duplex ? "full" : "half",
  635. phydev->pause == 1 ? "rx&tx" : "off");
  636. pr_cont("\n");
  637. }
  638. }
  639. /*
  640. * link changed callback (if phylib is not used)
  641. */
  642. static void bcm_enet_adjust_link(struct net_device *dev)
  643. {
  644. struct bcm_enet_priv *priv;
  645. priv = netdev_priv(dev);
  646. bcm_enet_set_duplex(priv, priv->force_duplex_full);
  647. bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
  648. netif_carrier_on(dev);
  649. pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
  650. dev->name,
  651. priv->force_speed_100 ? 100 : 10,
  652. priv->force_duplex_full ? "full" : "half",
  653. priv->pause_rx ? "rx" : "off",
  654. priv->pause_tx ? "tx" : "off");
  655. }
  656. /*
  657. * open callback, allocate dma rings & buffers and start rx operation
  658. */
  659. static int bcm_enet_open(struct net_device *dev)
  660. {
  661. struct bcm_enet_priv *priv;
  662. struct sockaddr addr;
  663. struct device *kdev;
  664. struct phy_device *phydev;
  665. int i, ret;
  666. unsigned int size;
  667. char phy_id[MII_BUS_ID_SIZE + 3];
  668. void *p;
  669. u32 val;
  670. priv = netdev_priv(dev);
  671. kdev = &priv->pdev->dev;
  672. if (priv->has_phy) {
  673. /* connect to PHY */
  674. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  675. priv->mii_bus->id, priv->phy_id);
  676. phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
  677. PHY_INTERFACE_MODE_MII);
  678. if (IS_ERR(phydev)) {
  679. dev_err(kdev, "could not attach to PHY\n");
  680. return PTR_ERR(phydev);
  681. }
  682. /* mask with MAC supported features */
  683. phydev->supported &= (SUPPORTED_10baseT_Half |
  684. SUPPORTED_10baseT_Full |
  685. SUPPORTED_100baseT_Half |
  686. SUPPORTED_100baseT_Full |
  687. SUPPORTED_Autoneg |
  688. SUPPORTED_Pause |
  689. SUPPORTED_MII);
  690. phydev->advertising = phydev->supported;
  691. if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
  692. phydev->advertising |= SUPPORTED_Pause;
  693. else
  694. phydev->advertising &= ~SUPPORTED_Pause;
  695. dev_info(kdev, "attached PHY at address %d [%s]\n",
  696. phydev->addr, phydev->drv->name);
  697. priv->old_link = 0;
  698. priv->old_duplex = -1;
  699. priv->old_pause = -1;
  700. priv->phydev = phydev;
  701. }
  702. /* mask all interrupts and request them */
  703. enet_writel(priv, 0, ENET_IRMASK_REG);
  704. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->rx_chan));
  705. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->tx_chan));
  706. ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
  707. if (ret)
  708. goto out_phy_disconnect;
  709. ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, IRQF_DISABLED,
  710. dev->name, dev);
  711. if (ret)
  712. goto out_freeirq;
  713. ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
  714. IRQF_DISABLED, dev->name, dev);
  715. if (ret)
  716. goto out_freeirq_rx;
  717. /* initialize perfect match registers */
  718. for (i = 0; i < 4; i++) {
  719. enet_writel(priv, 0, ENET_PML_REG(i));
  720. enet_writel(priv, 0, ENET_PMH_REG(i));
  721. }
  722. /* write device mac address */
  723. memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
  724. bcm_enet_set_mac_address(dev, &addr);
  725. /* allocate rx dma ring */
  726. size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
  727. p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma,
  728. GFP_KERNEL | __GFP_ZERO);
  729. if (!p) {
  730. ret = -ENOMEM;
  731. goto out_freeirq_tx;
  732. }
  733. priv->rx_desc_alloc_size = size;
  734. priv->rx_desc_cpu = p;
  735. /* allocate tx dma ring */
  736. size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
  737. p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma,
  738. GFP_KERNEL | __GFP_ZERO);
  739. if (!p) {
  740. ret = -ENOMEM;
  741. goto out_free_rx_ring;
  742. }
  743. priv->tx_desc_alloc_size = size;
  744. priv->tx_desc_cpu = p;
  745. priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
  746. GFP_KERNEL);
  747. if (!priv->tx_skb) {
  748. ret = -ENOMEM;
  749. goto out_free_tx_ring;
  750. }
  751. priv->tx_desc_count = priv->tx_ring_size;
  752. priv->tx_dirty_desc = 0;
  753. priv->tx_curr_desc = 0;
  754. spin_lock_init(&priv->tx_lock);
  755. /* init & fill rx ring with skbs */
  756. priv->rx_skb = kcalloc(priv->rx_ring_size, sizeof(struct sk_buff *),
  757. GFP_KERNEL);
  758. if (!priv->rx_skb) {
  759. ret = -ENOMEM;
  760. goto out_free_tx_skb;
  761. }
  762. priv->rx_desc_count = 0;
  763. priv->rx_dirty_desc = 0;
  764. priv->rx_curr_desc = 0;
  765. /* initialize flow control buffer allocation */
  766. enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
  767. ENETDMA_BUFALLOC_REG(priv->rx_chan));
  768. if (bcm_enet_refill_rx(dev)) {
  769. dev_err(kdev, "cannot allocate rx skb queue\n");
  770. ret = -ENOMEM;
  771. goto out;
  772. }
  773. /* write rx & tx ring addresses */
  774. enet_dmas_writel(priv, priv->rx_desc_dma,
  775. ENETDMAS_RSTART_REG(priv->rx_chan));
  776. enet_dmas_writel(priv, priv->tx_desc_dma,
  777. ENETDMAS_RSTART_REG(priv->tx_chan));
  778. /* clear remaining state ram for rx & tx channel */
  779. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG(priv->rx_chan));
  780. enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG(priv->tx_chan));
  781. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG(priv->rx_chan));
  782. enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG(priv->tx_chan));
  783. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG(priv->rx_chan));
  784. enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG(priv->tx_chan));
  785. /* set max rx/tx length */
  786. enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
  787. enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
  788. /* set dma maximum burst len */
  789. enet_dmac_writel(priv, BCMENET_DMA_MAXBURST,
  790. ENETDMAC_MAXBURST_REG(priv->rx_chan));
  791. enet_dmac_writel(priv, BCMENET_DMA_MAXBURST,
  792. ENETDMAC_MAXBURST_REG(priv->tx_chan));
  793. /* set correct transmit fifo watermark */
  794. enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
  795. /* set flow control low/high threshold to 1/3 / 2/3 */
  796. val = priv->rx_ring_size / 3;
  797. enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
  798. val = (priv->rx_ring_size * 2) / 3;
  799. enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
  800. /* all set, enable mac and interrupts, start dma engine and
  801. * kick rx dma channel */
  802. wmb();
  803. val = enet_readl(priv, ENET_CTL_REG);
  804. val |= ENET_CTL_ENABLE_MASK;
  805. enet_writel(priv, val, ENET_CTL_REG);
  806. enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
  807. enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
  808. ENETDMAC_CHANCFG_REG(priv->rx_chan));
  809. /* watch "mib counters about to overflow" interrupt */
  810. enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
  811. enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
  812. /* watch "packet transferred" interrupt in rx and tx */
  813. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  814. ENETDMAC_IR_REG(priv->rx_chan));
  815. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  816. ENETDMAC_IR_REG(priv->tx_chan));
  817. /* make sure we enable napi before rx interrupt */
  818. napi_enable(&priv->napi);
  819. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  820. ENETDMAC_IRMASK_REG(priv->rx_chan));
  821. enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
  822. ENETDMAC_IRMASK_REG(priv->tx_chan));
  823. if (priv->has_phy)
  824. phy_start(priv->phydev);
  825. else
  826. bcm_enet_adjust_link(dev);
  827. netif_start_queue(dev);
  828. return 0;
  829. out:
  830. for (i = 0; i < priv->rx_ring_size; i++) {
  831. struct bcm_enet_desc *desc;
  832. if (!priv->rx_skb[i])
  833. continue;
  834. desc = &priv->rx_desc_cpu[i];
  835. dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
  836. DMA_FROM_DEVICE);
  837. kfree_skb(priv->rx_skb[i]);
  838. }
  839. kfree(priv->rx_skb);
  840. out_free_tx_skb:
  841. kfree(priv->tx_skb);
  842. out_free_tx_ring:
  843. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  844. priv->tx_desc_cpu, priv->tx_desc_dma);
  845. out_free_rx_ring:
  846. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  847. priv->rx_desc_cpu, priv->rx_desc_dma);
  848. out_freeirq_tx:
  849. free_irq(priv->irq_tx, dev);
  850. out_freeirq_rx:
  851. free_irq(priv->irq_rx, dev);
  852. out_freeirq:
  853. free_irq(dev->irq, dev);
  854. out_phy_disconnect:
  855. phy_disconnect(priv->phydev);
  856. return ret;
  857. }
  858. /*
  859. * disable mac
  860. */
  861. static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
  862. {
  863. int limit;
  864. u32 val;
  865. val = enet_readl(priv, ENET_CTL_REG);
  866. val |= ENET_CTL_DISABLE_MASK;
  867. enet_writel(priv, val, ENET_CTL_REG);
  868. limit = 1000;
  869. do {
  870. u32 val;
  871. val = enet_readl(priv, ENET_CTL_REG);
  872. if (!(val & ENET_CTL_DISABLE_MASK))
  873. break;
  874. udelay(1);
  875. } while (limit--);
  876. }
  877. /*
  878. * disable dma in given channel
  879. */
  880. static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
  881. {
  882. int limit;
  883. enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG_REG(chan));
  884. limit = 1000;
  885. do {
  886. u32 val;
  887. val = enet_dmac_readl(priv, ENETDMAC_CHANCFG_REG(chan));
  888. if (!(val & ENETDMAC_CHANCFG_EN_MASK))
  889. break;
  890. udelay(1);
  891. } while (limit--);
  892. }
  893. /*
  894. * stop callback
  895. */
  896. static int bcm_enet_stop(struct net_device *dev)
  897. {
  898. struct bcm_enet_priv *priv;
  899. struct device *kdev;
  900. int i;
  901. priv = netdev_priv(dev);
  902. kdev = &priv->pdev->dev;
  903. netif_stop_queue(dev);
  904. napi_disable(&priv->napi);
  905. if (priv->has_phy)
  906. phy_stop(priv->phydev);
  907. del_timer_sync(&priv->rx_timeout);
  908. /* mask all interrupts */
  909. enet_writel(priv, 0, ENET_IRMASK_REG);
  910. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->rx_chan));
  911. enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->tx_chan));
  912. /* make sure no mib update is scheduled */
  913. cancel_work_sync(&priv->mib_update_task);
  914. /* disable dma & mac */
  915. bcm_enet_disable_dma(priv, priv->tx_chan);
  916. bcm_enet_disable_dma(priv, priv->rx_chan);
  917. bcm_enet_disable_mac(priv);
  918. /* force reclaim of all tx buffers */
  919. bcm_enet_tx_reclaim(dev, 1);
  920. /* free the rx skb ring */
  921. for (i = 0; i < priv->rx_ring_size; i++) {
  922. struct bcm_enet_desc *desc;
  923. if (!priv->rx_skb[i])
  924. continue;
  925. desc = &priv->rx_desc_cpu[i];
  926. dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
  927. DMA_FROM_DEVICE);
  928. kfree_skb(priv->rx_skb[i]);
  929. }
  930. /* free remaining allocated memory */
  931. kfree(priv->rx_skb);
  932. kfree(priv->tx_skb);
  933. dma_free_coherent(kdev, priv->rx_desc_alloc_size,
  934. priv->rx_desc_cpu, priv->rx_desc_dma);
  935. dma_free_coherent(kdev, priv->tx_desc_alloc_size,
  936. priv->tx_desc_cpu, priv->tx_desc_dma);
  937. free_irq(priv->irq_tx, dev);
  938. free_irq(priv->irq_rx, dev);
  939. free_irq(dev->irq, dev);
  940. /* release phy */
  941. if (priv->has_phy) {
  942. phy_disconnect(priv->phydev);
  943. priv->phydev = NULL;
  944. }
  945. return 0;
  946. }
  947. /*
  948. * ethtool callbacks
  949. */
  950. struct bcm_enet_stats {
  951. char stat_string[ETH_GSTRING_LEN];
  952. int sizeof_stat;
  953. int stat_offset;
  954. int mib_reg;
  955. };
  956. #define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
  957. offsetof(struct bcm_enet_priv, m)
  958. #define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
  959. offsetof(struct net_device_stats, m)
  960. static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
  961. { "rx_packets", DEV_STAT(rx_packets), -1 },
  962. { "tx_packets", DEV_STAT(tx_packets), -1 },
  963. { "rx_bytes", DEV_STAT(rx_bytes), -1 },
  964. { "tx_bytes", DEV_STAT(tx_bytes), -1 },
  965. { "rx_errors", DEV_STAT(rx_errors), -1 },
  966. { "tx_errors", DEV_STAT(tx_errors), -1 },
  967. { "rx_dropped", DEV_STAT(rx_dropped), -1 },
  968. { "tx_dropped", DEV_STAT(tx_dropped), -1 },
  969. { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
  970. { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
  971. { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
  972. { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
  973. { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
  974. { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
  975. { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
  976. { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
  977. { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
  978. { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
  979. { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
  980. { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
  981. { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
  982. { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
  983. { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
  984. { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
  985. { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
  986. { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
  987. { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
  988. { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
  989. { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
  990. { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
  991. { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
  992. { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
  993. { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
  994. { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
  995. { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
  996. { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
  997. { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
  998. { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
  999. { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
  1000. { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
  1001. { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
  1002. { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
  1003. { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
  1004. { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
  1005. { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
  1006. { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
  1007. { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
  1008. { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
  1009. { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
  1010. { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
  1011. { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
  1012. };
  1013. #define BCM_ENET_STATS_LEN \
  1014. (sizeof(bcm_enet_gstrings_stats) / sizeof(struct bcm_enet_stats))
  1015. static const u32 unused_mib_regs[] = {
  1016. ETH_MIB_TX_ALL_OCTETS,
  1017. ETH_MIB_TX_ALL_PKTS,
  1018. ETH_MIB_RX_ALL_OCTETS,
  1019. ETH_MIB_RX_ALL_PKTS,
  1020. };
  1021. static void bcm_enet_get_drvinfo(struct net_device *netdev,
  1022. struct ethtool_drvinfo *drvinfo)
  1023. {
  1024. strlcpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
  1025. strlcpy(drvinfo->version, bcm_enet_driver_version,
  1026. sizeof(drvinfo->version));
  1027. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1028. strlcpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
  1029. drvinfo->n_stats = BCM_ENET_STATS_LEN;
  1030. }
  1031. static int bcm_enet_get_sset_count(struct net_device *netdev,
  1032. int string_set)
  1033. {
  1034. switch (string_set) {
  1035. case ETH_SS_STATS:
  1036. return BCM_ENET_STATS_LEN;
  1037. default:
  1038. return -EINVAL;
  1039. }
  1040. }
  1041. static void bcm_enet_get_strings(struct net_device *netdev,
  1042. u32 stringset, u8 *data)
  1043. {
  1044. int i;
  1045. switch (stringset) {
  1046. case ETH_SS_STATS:
  1047. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1048. memcpy(data + i * ETH_GSTRING_LEN,
  1049. bcm_enet_gstrings_stats[i].stat_string,
  1050. ETH_GSTRING_LEN);
  1051. }
  1052. break;
  1053. }
  1054. }
  1055. static void update_mib_counters(struct bcm_enet_priv *priv)
  1056. {
  1057. int i;
  1058. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1059. const struct bcm_enet_stats *s;
  1060. u32 val;
  1061. char *p;
  1062. s = &bcm_enet_gstrings_stats[i];
  1063. if (s->mib_reg == -1)
  1064. continue;
  1065. val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
  1066. p = (char *)priv + s->stat_offset;
  1067. if (s->sizeof_stat == sizeof(u64))
  1068. *(u64 *)p += val;
  1069. else
  1070. *(u32 *)p += val;
  1071. }
  1072. /* also empty unused mib counters to make sure mib counter
  1073. * overflow interrupt is cleared */
  1074. for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
  1075. (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
  1076. }
  1077. static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
  1078. {
  1079. struct bcm_enet_priv *priv;
  1080. priv = container_of(t, struct bcm_enet_priv, mib_update_task);
  1081. mutex_lock(&priv->mib_update_lock);
  1082. update_mib_counters(priv);
  1083. mutex_unlock(&priv->mib_update_lock);
  1084. /* reenable mib interrupt */
  1085. if (netif_running(priv->net_dev))
  1086. enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
  1087. }
  1088. static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
  1089. struct ethtool_stats *stats,
  1090. u64 *data)
  1091. {
  1092. struct bcm_enet_priv *priv;
  1093. int i;
  1094. priv = netdev_priv(netdev);
  1095. mutex_lock(&priv->mib_update_lock);
  1096. update_mib_counters(priv);
  1097. for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
  1098. const struct bcm_enet_stats *s;
  1099. char *p;
  1100. s = &bcm_enet_gstrings_stats[i];
  1101. if (s->mib_reg == -1)
  1102. p = (char *)&netdev->stats;
  1103. else
  1104. p = (char *)priv;
  1105. p += s->stat_offset;
  1106. data[i] = (s->sizeof_stat == sizeof(u64)) ?
  1107. *(u64 *)p : *(u32 *)p;
  1108. }
  1109. mutex_unlock(&priv->mib_update_lock);
  1110. }
  1111. static int bcm_enet_nway_reset(struct net_device *dev)
  1112. {
  1113. struct bcm_enet_priv *priv;
  1114. priv = netdev_priv(dev);
  1115. if (priv->has_phy) {
  1116. if (!priv->phydev)
  1117. return -ENODEV;
  1118. return genphy_restart_aneg(priv->phydev);
  1119. }
  1120. return -EOPNOTSUPP;
  1121. }
  1122. static int bcm_enet_get_settings(struct net_device *dev,
  1123. struct ethtool_cmd *cmd)
  1124. {
  1125. struct bcm_enet_priv *priv;
  1126. priv = netdev_priv(dev);
  1127. cmd->maxrxpkt = 0;
  1128. cmd->maxtxpkt = 0;
  1129. if (priv->has_phy) {
  1130. if (!priv->phydev)
  1131. return -ENODEV;
  1132. return phy_ethtool_gset(priv->phydev, cmd);
  1133. } else {
  1134. cmd->autoneg = 0;
  1135. ethtool_cmd_speed_set(cmd, ((priv->force_speed_100)
  1136. ? SPEED_100 : SPEED_10));
  1137. cmd->duplex = (priv->force_duplex_full) ?
  1138. DUPLEX_FULL : DUPLEX_HALF;
  1139. cmd->supported = ADVERTISED_10baseT_Half |
  1140. ADVERTISED_10baseT_Full |
  1141. ADVERTISED_100baseT_Half |
  1142. ADVERTISED_100baseT_Full;
  1143. cmd->advertising = 0;
  1144. cmd->port = PORT_MII;
  1145. cmd->transceiver = XCVR_EXTERNAL;
  1146. }
  1147. return 0;
  1148. }
  1149. static int bcm_enet_set_settings(struct net_device *dev,
  1150. struct ethtool_cmd *cmd)
  1151. {
  1152. struct bcm_enet_priv *priv;
  1153. priv = netdev_priv(dev);
  1154. if (priv->has_phy) {
  1155. if (!priv->phydev)
  1156. return -ENODEV;
  1157. return phy_ethtool_sset(priv->phydev, cmd);
  1158. } else {
  1159. if (cmd->autoneg ||
  1160. (cmd->speed != SPEED_100 && cmd->speed != SPEED_10) ||
  1161. cmd->port != PORT_MII)
  1162. return -EINVAL;
  1163. priv->force_speed_100 = (cmd->speed == SPEED_100) ? 1 : 0;
  1164. priv->force_duplex_full = (cmd->duplex == DUPLEX_FULL) ? 1 : 0;
  1165. if (netif_running(dev))
  1166. bcm_enet_adjust_link(dev);
  1167. return 0;
  1168. }
  1169. }
  1170. static void bcm_enet_get_ringparam(struct net_device *dev,
  1171. struct ethtool_ringparam *ering)
  1172. {
  1173. struct bcm_enet_priv *priv;
  1174. priv = netdev_priv(dev);
  1175. /* rx/tx ring is actually only limited by memory */
  1176. ering->rx_max_pending = 8192;
  1177. ering->tx_max_pending = 8192;
  1178. ering->rx_pending = priv->rx_ring_size;
  1179. ering->tx_pending = priv->tx_ring_size;
  1180. }
  1181. static int bcm_enet_set_ringparam(struct net_device *dev,
  1182. struct ethtool_ringparam *ering)
  1183. {
  1184. struct bcm_enet_priv *priv;
  1185. int was_running;
  1186. priv = netdev_priv(dev);
  1187. was_running = 0;
  1188. if (netif_running(dev)) {
  1189. bcm_enet_stop(dev);
  1190. was_running = 1;
  1191. }
  1192. priv->rx_ring_size = ering->rx_pending;
  1193. priv->tx_ring_size = ering->tx_pending;
  1194. if (was_running) {
  1195. int err;
  1196. err = bcm_enet_open(dev);
  1197. if (err)
  1198. dev_close(dev);
  1199. else
  1200. bcm_enet_set_multicast_list(dev);
  1201. }
  1202. return 0;
  1203. }
  1204. static void bcm_enet_get_pauseparam(struct net_device *dev,
  1205. struct ethtool_pauseparam *ecmd)
  1206. {
  1207. struct bcm_enet_priv *priv;
  1208. priv = netdev_priv(dev);
  1209. ecmd->autoneg = priv->pause_auto;
  1210. ecmd->rx_pause = priv->pause_rx;
  1211. ecmd->tx_pause = priv->pause_tx;
  1212. }
  1213. static int bcm_enet_set_pauseparam(struct net_device *dev,
  1214. struct ethtool_pauseparam *ecmd)
  1215. {
  1216. struct bcm_enet_priv *priv;
  1217. priv = netdev_priv(dev);
  1218. if (priv->has_phy) {
  1219. if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
  1220. /* asymetric pause mode not supported,
  1221. * actually possible but integrated PHY has RO
  1222. * asym_pause bit */
  1223. return -EINVAL;
  1224. }
  1225. } else {
  1226. /* no pause autoneg on direct mii connection */
  1227. if (ecmd->autoneg)
  1228. return -EINVAL;
  1229. }
  1230. priv->pause_auto = ecmd->autoneg;
  1231. priv->pause_rx = ecmd->rx_pause;
  1232. priv->pause_tx = ecmd->tx_pause;
  1233. return 0;
  1234. }
  1235. static const struct ethtool_ops bcm_enet_ethtool_ops = {
  1236. .get_strings = bcm_enet_get_strings,
  1237. .get_sset_count = bcm_enet_get_sset_count,
  1238. .get_ethtool_stats = bcm_enet_get_ethtool_stats,
  1239. .nway_reset = bcm_enet_nway_reset,
  1240. .get_settings = bcm_enet_get_settings,
  1241. .set_settings = bcm_enet_set_settings,
  1242. .get_drvinfo = bcm_enet_get_drvinfo,
  1243. .get_link = ethtool_op_get_link,
  1244. .get_ringparam = bcm_enet_get_ringparam,
  1245. .set_ringparam = bcm_enet_set_ringparam,
  1246. .get_pauseparam = bcm_enet_get_pauseparam,
  1247. .set_pauseparam = bcm_enet_set_pauseparam,
  1248. };
  1249. static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1250. {
  1251. struct bcm_enet_priv *priv;
  1252. priv = netdev_priv(dev);
  1253. if (priv->has_phy) {
  1254. if (!priv->phydev)
  1255. return -ENODEV;
  1256. return phy_mii_ioctl(priv->phydev, rq, cmd);
  1257. } else {
  1258. struct mii_if_info mii;
  1259. mii.dev = dev;
  1260. mii.mdio_read = bcm_enet_mdio_read_mii;
  1261. mii.mdio_write = bcm_enet_mdio_write_mii;
  1262. mii.phy_id = 0;
  1263. mii.phy_id_mask = 0x3f;
  1264. mii.reg_num_mask = 0x1f;
  1265. return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
  1266. }
  1267. }
  1268. /*
  1269. * calculate actual hardware mtu
  1270. */
  1271. static int compute_hw_mtu(struct bcm_enet_priv *priv, int mtu)
  1272. {
  1273. int actual_mtu;
  1274. actual_mtu = mtu;
  1275. /* add ethernet header + vlan tag size */
  1276. actual_mtu += VLAN_ETH_HLEN;
  1277. if (actual_mtu < 64 || actual_mtu > BCMENET_MAX_MTU)
  1278. return -EINVAL;
  1279. /*
  1280. * setup maximum size before we get overflow mark in
  1281. * descriptor, note that this will not prevent reception of
  1282. * big frames, they will be split into multiple buffers
  1283. * anyway
  1284. */
  1285. priv->hw_mtu = actual_mtu;
  1286. /*
  1287. * align rx buffer size to dma burst len, account FCS since
  1288. * it's appended
  1289. */
  1290. priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
  1291. BCMENET_DMA_MAXBURST * 4);
  1292. return 0;
  1293. }
  1294. /*
  1295. * adjust mtu, can't be called while device is running
  1296. */
  1297. static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
  1298. {
  1299. int ret;
  1300. if (netif_running(dev))
  1301. return -EBUSY;
  1302. ret = compute_hw_mtu(netdev_priv(dev), new_mtu);
  1303. if (ret)
  1304. return ret;
  1305. dev->mtu = new_mtu;
  1306. return 0;
  1307. }
  1308. /*
  1309. * preinit hardware to allow mii operation while device is down
  1310. */
  1311. static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
  1312. {
  1313. u32 val;
  1314. int limit;
  1315. /* make sure mac is disabled */
  1316. bcm_enet_disable_mac(priv);
  1317. /* soft reset mac */
  1318. val = ENET_CTL_SRESET_MASK;
  1319. enet_writel(priv, val, ENET_CTL_REG);
  1320. wmb();
  1321. limit = 1000;
  1322. do {
  1323. val = enet_readl(priv, ENET_CTL_REG);
  1324. if (!(val & ENET_CTL_SRESET_MASK))
  1325. break;
  1326. udelay(1);
  1327. } while (limit--);
  1328. /* select correct mii interface */
  1329. val = enet_readl(priv, ENET_CTL_REG);
  1330. if (priv->use_external_mii)
  1331. val |= ENET_CTL_EPHYSEL_MASK;
  1332. else
  1333. val &= ~ENET_CTL_EPHYSEL_MASK;
  1334. enet_writel(priv, val, ENET_CTL_REG);
  1335. /* turn on mdc clock */
  1336. enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
  1337. ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
  1338. /* set mib counters to self-clear when read */
  1339. val = enet_readl(priv, ENET_MIBCTL_REG);
  1340. val |= ENET_MIBCTL_RDCLEAR_MASK;
  1341. enet_writel(priv, val, ENET_MIBCTL_REG);
  1342. }
  1343. static const struct net_device_ops bcm_enet_ops = {
  1344. .ndo_open = bcm_enet_open,
  1345. .ndo_stop = bcm_enet_stop,
  1346. .ndo_start_xmit = bcm_enet_start_xmit,
  1347. .ndo_set_mac_address = bcm_enet_set_mac_address,
  1348. .ndo_set_rx_mode = bcm_enet_set_multicast_list,
  1349. .ndo_do_ioctl = bcm_enet_ioctl,
  1350. .ndo_change_mtu = bcm_enet_change_mtu,
  1351. #ifdef CONFIG_NET_POLL_CONTROLLER
  1352. .ndo_poll_controller = bcm_enet_netpoll,
  1353. #endif
  1354. };
  1355. /*
  1356. * allocate netdevice, request register memory and register device.
  1357. */
  1358. static int bcm_enet_probe(struct platform_device *pdev)
  1359. {
  1360. struct bcm_enet_priv *priv;
  1361. struct net_device *dev;
  1362. struct bcm63xx_enet_platform_data *pd;
  1363. struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
  1364. struct mii_bus *bus;
  1365. const char *clk_name;
  1366. int i, ret;
  1367. /* stop if shared driver failed, assume driver->probe will be
  1368. * called in the same order we register devices (correct ?) */
  1369. if (!bcm_enet_shared_base[0])
  1370. return -ENODEV;
  1371. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1372. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1373. res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1374. res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  1375. if (!res_mem || !res_irq || !res_irq_rx || !res_irq_tx)
  1376. return -ENODEV;
  1377. ret = 0;
  1378. dev = alloc_etherdev(sizeof(*priv));
  1379. if (!dev)
  1380. return -ENOMEM;
  1381. priv = netdev_priv(dev);
  1382. ret = compute_hw_mtu(priv, dev->mtu);
  1383. if (ret)
  1384. goto out;
  1385. priv->base = devm_request_and_ioremap(&pdev->dev, res_mem);
  1386. if (priv->base == NULL) {
  1387. ret = -ENOMEM;
  1388. goto out;
  1389. }
  1390. dev->irq = priv->irq = res_irq->start;
  1391. priv->irq_rx = res_irq_rx->start;
  1392. priv->irq_tx = res_irq_tx->start;
  1393. priv->mac_id = pdev->id;
  1394. /* get rx & tx dma channel id for this mac */
  1395. if (priv->mac_id == 0) {
  1396. priv->rx_chan = 0;
  1397. priv->tx_chan = 1;
  1398. clk_name = "enet0";
  1399. } else {
  1400. priv->rx_chan = 2;
  1401. priv->tx_chan = 3;
  1402. clk_name = "enet1";
  1403. }
  1404. priv->mac_clk = clk_get(&pdev->dev, clk_name);
  1405. if (IS_ERR(priv->mac_clk)) {
  1406. ret = PTR_ERR(priv->mac_clk);
  1407. goto out;
  1408. }
  1409. clk_prepare_enable(priv->mac_clk);
  1410. /* initialize default and fetch platform data */
  1411. priv->rx_ring_size = BCMENET_DEF_RX_DESC;
  1412. priv->tx_ring_size = BCMENET_DEF_TX_DESC;
  1413. pd = pdev->dev.platform_data;
  1414. if (pd) {
  1415. memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
  1416. priv->has_phy = pd->has_phy;
  1417. priv->phy_id = pd->phy_id;
  1418. priv->has_phy_interrupt = pd->has_phy_interrupt;
  1419. priv->phy_interrupt = pd->phy_interrupt;
  1420. priv->use_external_mii = !pd->use_internal_phy;
  1421. priv->pause_auto = pd->pause_auto;
  1422. priv->pause_rx = pd->pause_rx;
  1423. priv->pause_tx = pd->pause_tx;
  1424. priv->force_duplex_full = pd->force_duplex_full;
  1425. priv->force_speed_100 = pd->force_speed_100;
  1426. }
  1427. if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
  1428. /* using internal PHY, enable clock */
  1429. priv->phy_clk = clk_get(&pdev->dev, "ephy");
  1430. if (IS_ERR(priv->phy_clk)) {
  1431. ret = PTR_ERR(priv->phy_clk);
  1432. priv->phy_clk = NULL;
  1433. goto out_put_clk_mac;
  1434. }
  1435. clk_prepare_enable(priv->phy_clk);
  1436. }
  1437. /* do minimal hardware init to be able to probe mii bus */
  1438. bcm_enet_hw_preinit(priv);
  1439. /* MII bus registration */
  1440. if (priv->has_phy) {
  1441. priv->mii_bus = mdiobus_alloc();
  1442. if (!priv->mii_bus) {
  1443. ret = -ENOMEM;
  1444. goto out_uninit_hw;
  1445. }
  1446. bus = priv->mii_bus;
  1447. bus->name = "bcm63xx_enet MII bus";
  1448. bus->parent = &pdev->dev;
  1449. bus->priv = priv;
  1450. bus->read = bcm_enet_mdio_read_phylib;
  1451. bus->write = bcm_enet_mdio_write_phylib;
  1452. sprintf(bus->id, "%s-%d", pdev->name, priv->mac_id);
  1453. /* only probe bus where we think the PHY is, because
  1454. * the mdio read operation return 0 instead of 0xffff
  1455. * if a slave is not present on hw */
  1456. bus->phy_mask = ~(1 << priv->phy_id);
  1457. bus->irq = devm_kzalloc(&pdev->dev, sizeof(int) * PHY_MAX_ADDR,
  1458. GFP_KERNEL);
  1459. if (!bus->irq) {
  1460. ret = -ENOMEM;
  1461. goto out_free_mdio;
  1462. }
  1463. if (priv->has_phy_interrupt)
  1464. bus->irq[priv->phy_id] = priv->phy_interrupt;
  1465. else
  1466. bus->irq[priv->phy_id] = PHY_POLL;
  1467. ret = mdiobus_register(bus);
  1468. if (ret) {
  1469. dev_err(&pdev->dev, "unable to register mdio bus\n");
  1470. goto out_free_mdio;
  1471. }
  1472. } else {
  1473. /* run platform code to initialize PHY device */
  1474. if (pd->mii_config &&
  1475. pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
  1476. bcm_enet_mdio_write_mii)) {
  1477. dev_err(&pdev->dev, "unable to configure mdio bus\n");
  1478. goto out_uninit_hw;
  1479. }
  1480. }
  1481. spin_lock_init(&priv->rx_lock);
  1482. /* init rx timeout (used for oom) */
  1483. init_timer(&priv->rx_timeout);
  1484. priv->rx_timeout.function = bcm_enet_refill_rx_timer;
  1485. priv->rx_timeout.data = (unsigned long)dev;
  1486. /* init the mib update lock&work */
  1487. mutex_init(&priv->mib_update_lock);
  1488. INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
  1489. /* zero mib counters */
  1490. for (i = 0; i < ENET_MIB_REG_COUNT; i++)
  1491. enet_writel(priv, 0, ENET_MIB_REG(i));
  1492. /* register netdevice */
  1493. dev->netdev_ops = &bcm_enet_ops;
  1494. netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
  1495. SET_ETHTOOL_OPS(dev, &bcm_enet_ethtool_ops);
  1496. SET_NETDEV_DEV(dev, &pdev->dev);
  1497. ret = register_netdev(dev);
  1498. if (ret)
  1499. goto out_unregister_mdio;
  1500. netif_carrier_off(dev);
  1501. platform_set_drvdata(pdev, dev);
  1502. priv->pdev = pdev;
  1503. priv->net_dev = dev;
  1504. return 0;
  1505. out_unregister_mdio:
  1506. if (priv->mii_bus)
  1507. mdiobus_unregister(priv->mii_bus);
  1508. out_free_mdio:
  1509. if (priv->mii_bus)
  1510. mdiobus_free(priv->mii_bus);
  1511. out_uninit_hw:
  1512. /* turn off mdc clock */
  1513. enet_writel(priv, 0, ENET_MIISC_REG);
  1514. if (priv->phy_clk) {
  1515. clk_disable_unprepare(priv->phy_clk);
  1516. clk_put(priv->phy_clk);
  1517. }
  1518. out_put_clk_mac:
  1519. clk_disable_unprepare(priv->mac_clk);
  1520. clk_put(priv->mac_clk);
  1521. out:
  1522. free_netdev(dev);
  1523. return ret;
  1524. }
  1525. /*
  1526. * exit func, stops hardware and unregisters netdevice
  1527. */
  1528. static int bcm_enet_remove(struct platform_device *pdev)
  1529. {
  1530. struct bcm_enet_priv *priv;
  1531. struct net_device *dev;
  1532. /* stop netdevice */
  1533. dev = platform_get_drvdata(pdev);
  1534. priv = netdev_priv(dev);
  1535. unregister_netdev(dev);
  1536. /* turn off mdc clock */
  1537. enet_writel(priv, 0, ENET_MIISC_REG);
  1538. if (priv->has_phy) {
  1539. mdiobus_unregister(priv->mii_bus);
  1540. mdiobus_free(priv->mii_bus);
  1541. } else {
  1542. struct bcm63xx_enet_platform_data *pd;
  1543. pd = pdev->dev.platform_data;
  1544. if (pd && pd->mii_config)
  1545. pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
  1546. bcm_enet_mdio_write_mii);
  1547. }
  1548. /* disable hw block clocks */
  1549. if (priv->phy_clk) {
  1550. clk_disable_unprepare(priv->phy_clk);
  1551. clk_put(priv->phy_clk);
  1552. }
  1553. clk_disable_unprepare(priv->mac_clk);
  1554. clk_put(priv->mac_clk);
  1555. free_netdev(dev);
  1556. return 0;
  1557. }
  1558. struct platform_driver bcm63xx_enet_driver = {
  1559. .probe = bcm_enet_probe,
  1560. .remove = bcm_enet_remove,
  1561. .driver = {
  1562. .name = "bcm63xx_enet",
  1563. .owner = THIS_MODULE,
  1564. },
  1565. };
  1566. /*
  1567. * reserve & remap memory space shared between all macs
  1568. */
  1569. static int bcm_enet_shared_probe(struct platform_device *pdev)
  1570. {
  1571. struct resource *res;
  1572. void __iomem *p[3];
  1573. unsigned int i;
  1574. memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
  1575. for (i = 0; i < 3; i++) {
  1576. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  1577. p[i] = devm_ioremap_resource(&pdev->dev, res);
  1578. if (!p[i])
  1579. return -ENOMEM;
  1580. }
  1581. memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
  1582. return 0;
  1583. }
  1584. static int bcm_enet_shared_remove(struct platform_device *pdev)
  1585. {
  1586. return 0;
  1587. }
  1588. /*
  1589. * this "shared" driver is needed because both macs share a single
  1590. * address space
  1591. */
  1592. struct platform_driver bcm63xx_enet_shared_driver = {
  1593. .probe = bcm_enet_shared_probe,
  1594. .remove = bcm_enet_shared_remove,
  1595. .driver = {
  1596. .name = "bcm63xx_enet_shared",
  1597. .owner = THIS_MODULE,
  1598. },
  1599. };
  1600. /*
  1601. * entry point
  1602. */
  1603. static int __init bcm_enet_init(void)
  1604. {
  1605. int ret;
  1606. ret = platform_driver_register(&bcm63xx_enet_shared_driver);
  1607. if (ret)
  1608. return ret;
  1609. ret = platform_driver_register(&bcm63xx_enet_driver);
  1610. if (ret)
  1611. platform_driver_unregister(&bcm63xx_enet_shared_driver);
  1612. return ret;
  1613. }
  1614. static void __exit bcm_enet_exit(void)
  1615. {
  1616. platform_driver_unregister(&bcm63xx_enet_driver);
  1617. platform_driver_unregister(&bcm63xx_enet_shared_driver);
  1618. }
  1619. module_init(bcm_enet_init);
  1620. module_exit(bcm_enet_exit);
  1621. MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
  1622. MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
  1623. MODULE_LICENSE("GPL");