bcm63xx_enet.c 48 KB

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