bfin_mac.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  1. /*
  2. * Blackfin On-Chip MAC Driver
  3. *
  4. * Copyright 2004-2007 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/timer.h>
  17. #include <linux/errno.h>
  18. #include <linux/irq.h>
  19. #include <linux/io.h>
  20. #include <linux/ioport.h>
  21. #include <linux/crc32.h>
  22. #include <linux/device.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mii.h>
  25. #include <linux/phy.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/platform_device.h>
  31. #include <asm/dma.h>
  32. #include <linux/dma-mapping.h>
  33. #include <asm/div64.h>
  34. #include <asm/dpmc.h>
  35. #include <asm/blackfin.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/portmux.h>
  38. #include <mach/pll.h>
  39. #include "bfin_mac.h"
  40. #define DRV_NAME "bfin_mac"
  41. #define DRV_VERSION "1.1"
  42. #define DRV_AUTHOR "Bryan Wu, Luke Yang"
  43. #define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
  44. MODULE_AUTHOR(DRV_AUTHOR);
  45. MODULE_LICENSE("GPL");
  46. MODULE_DESCRIPTION(DRV_DESC);
  47. MODULE_ALIAS("platform:bfin_mac");
  48. #if defined(CONFIG_BFIN_MAC_USE_L1)
  49. # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
  50. # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
  51. #else
  52. # define bfin_mac_alloc(dma_handle, size) \
  53. dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
  54. # define bfin_mac_free(dma_handle, ptr) \
  55. dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
  56. #endif
  57. #define PKT_BUF_SZ 1580
  58. #define MAX_TIMEOUT_CNT 500
  59. /* pointers to maintain transmit list */
  60. static struct net_dma_desc_tx *tx_list_head;
  61. static struct net_dma_desc_tx *tx_list_tail;
  62. static struct net_dma_desc_rx *rx_list_head;
  63. static struct net_dma_desc_rx *rx_list_tail;
  64. static struct net_dma_desc_rx *current_rx_ptr;
  65. static struct net_dma_desc_tx *current_tx_ptr;
  66. static struct net_dma_desc_tx *tx_desc;
  67. static struct net_dma_desc_rx *rx_desc;
  68. #if defined(CONFIG_BFIN_MAC_RMII)
  69. static u16 pin_req[] = P_RMII0;
  70. #else
  71. static u16 pin_req[] = P_MII0;
  72. #endif
  73. static void desc_list_free(void)
  74. {
  75. struct net_dma_desc_rx *r;
  76. struct net_dma_desc_tx *t;
  77. int i;
  78. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  79. dma_addr_t dma_handle = 0;
  80. #endif
  81. if (tx_desc) {
  82. t = tx_list_head;
  83. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  84. if (t) {
  85. if (t->skb) {
  86. dev_kfree_skb(t->skb);
  87. t->skb = NULL;
  88. }
  89. t = t->next;
  90. }
  91. }
  92. bfin_mac_free(dma_handle, tx_desc);
  93. }
  94. if (rx_desc) {
  95. r = rx_list_head;
  96. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  97. if (r) {
  98. if (r->skb) {
  99. dev_kfree_skb(r->skb);
  100. r->skb = NULL;
  101. }
  102. r = r->next;
  103. }
  104. }
  105. bfin_mac_free(dma_handle, rx_desc);
  106. }
  107. }
  108. static int desc_list_init(void)
  109. {
  110. int i;
  111. struct sk_buff *new_skb;
  112. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  113. /*
  114. * This dma_handle is useless in Blackfin dma_alloc_coherent().
  115. * The real dma handler is the return value of dma_alloc_coherent().
  116. */
  117. dma_addr_t dma_handle;
  118. #endif
  119. tx_desc = bfin_mac_alloc(&dma_handle,
  120. sizeof(struct net_dma_desc_tx) *
  121. CONFIG_BFIN_TX_DESC_NUM);
  122. if (tx_desc == NULL)
  123. goto init_error;
  124. rx_desc = bfin_mac_alloc(&dma_handle,
  125. sizeof(struct net_dma_desc_rx) *
  126. CONFIG_BFIN_RX_DESC_NUM);
  127. if (rx_desc == NULL)
  128. goto init_error;
  129. /* init tx_list */
  130. tx_list_head = tx_list_tail = tx_desc;
  131. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  132. struct net_dma_desc_tx *t = tx_desc + i;
  133. struct dma_descriptor *a = &(t->desc_a);
  134. struct dma_descriptor *b = &(t->desc_b);
  135. /*
  136. * disable DMA
  137. * read from memory WNR = 0
  138. * wordsize is 32 bits
  139. * 6 half words is desc size
  140. * large desc flow
  141. */
  142. a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  143. a->start_addr = (unsigned long)t->packet;
  144. a->x_count = 0;
  145. a->next_dma_desc = b;
  146. /*
  147. * enabled DMA
  148. * write to memory WNR = 1
  149. * wordsize is 32 bits
  150. * disable interrupt
  151. * 6 half words is desc size
  152. * large desc flow
  153. */
  154. b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  155. b->start_addr = (unsigned long)(&(t->status));
  156. b->x_count = 0;
  157. t->skb = NULL;
  158. tx_list_tail->desc_b.next_dma_desc = a;
  159. tx_list_tail->next = t;
  160. tx_list_tail = t;
  161. }
  162. tx_list_tail->next = tx_list_head; /* tx_list is a circle */
  163. tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
  164. current_tx_ptr = tx_list_head;
  165. /* init rx_list */
  166. rx_list_head = rx_list_tail = rx_desc;
  167. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  168. struct net_dma_desc_rx *r = rx_desc + i;
  169. struct dma_descriptor *a = &(r->desc_a);
  170. struct dma_descriptor *b = &(r->desc_b);
  171. /* allocate a new skb for next time receive */
  172. new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
  173. if (!new_skb) {
  174. printk(KERN_NOTICE DRV_NAME
  175. ": init: low on mem - packet dropped\n");
  176. goto init_error;
  177. }
  178. skb_reserve(new_skb, NET_IP_ALIGN);
  179. /* Invidate the data cache of skb->data range when it is write back
  180. * cache. It will prevent overwritting the new data from DMA
  181. */
  182. blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
  183. (unsigned long)new_skb->end);
  184. r->skb = new_skb;
  185. /*
  186. * enabled DMA
  187. * write to memory WNR = 1
  188. * wordsize is 32 bits
  189. * disable interrupt
  190. * 6 half words is desc size
  191. * large desc flow
  192. */
  193. a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  194. /* since RXDWA is enabled */
  195. a->start_addr = (unsigned long)new_skb->data - 2;
  196. a->x_count = 0;
  197. a->next_dma_desc = b;
  198. /*
  199. * enabled DMA
  200. * write to memory WNR = 1
  201. * wordsize is 32 bits
  202. * enable interrupt
  203. * 6 half words is desc size
  204. * large desc flow
  205. */
  206. b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
  207. NDSIZE_6 | DMAFLOW_LARGE;
  208. b->start_addr = (unsigned long)(&(r->status));
  209. b->x_count = 0;
  210. rx_list_tail->desc_b.next_dma_desc = a;
  211. rx_list_tail->next = r;
  212. rx_list_tail = r;
  213. }
  214. rx_list_tail->next = rx_list_head; /* rx_list is a circle */
  215. rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
  216. current_rx_ptr = rx_list_head;
  217. return 0;
  218. init_error:
  219. desc_list_free();
  220. printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
  221. return -ENOMEM;
  222. }
  223. /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
  224. /*
  225. * MII operations
  226. */
  227. /* Wait until the previous MDC/MDIO transaction has completed */
  228. static int bfin_mdio_poll(void)
  229. {
  230. int timeout_cnt = MAX_TIMEOUT_CNT;
  231. /* poll the STABUSY bit */
  232. while ((bfin_read_EMAC_STAADD()) & STABUSY) {
  233. udelay(1);
  234. if (timeout_cnt-- < 0) {
  235. printk(KERN_ERR DRV_NAME
  236. ": wait MDC/MDIO transaction to complete timeout\n");
  237. return -ETIMEDOUT;
  238. }
  239. }
  240. return 0;
  241. }
  242. /* Read an off-chip register in a PHY through the MDC/MDIO port */
  243. static int bfin_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  244. {
  245. int ret;
  246. ret = bfin_mdio_poll();
  247. if (ret)
  248. return ret;
  249. /* read mode */
  250. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  251. SET_REGAD((u16) regnum) |
  252. STABUSY);
  253. ret = bfin_mdio_poll();
  254. if (ret)
  255. return ret;
  256. return (int) bfin_read_EMAC_STADAT();
  257. }
  258. /* Write an off-chip register in a PHY through the MDC/MDIO port */
  259. static int bfin_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  260. u16 value)
  261. {
  262. int ret;
  263. ret = bfin_mdio_poll();
  264. if (ret)
  265. return ret;
  266. bfin_write_EMAC_STADAT((u32) value);
  267. /* write mode */
  268. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  269. SET_REGAD((u16) regnum) |
  270. STAOP |
  271. STABUSY);
  272. return bfin_mdio_poll();
  273. }
  274. static int bfin_mdiobus_reset(struct mii_bus *bus)
  275. {
  276. return 0;
  277. }
  278. static void bfin_mac_adjust_link(struct net_device *dev)
  279. {
  280. struct bfin_mac_local *lp = netdev_priv(dev);
  281. struct phy_device *phydev = lp->phydev;
  282. unsigned long flags;
  283. int new_state = 0;
  284. spin_lock_irqsave(&lp->lock, flags);
  285. if (phydev->link) {
  286. /* Now we make sure that we can be in full duplex mode.
  287. * If not, we operate in half-duplex mode. */
  288. if (phydev->duplex != lp->old_duplex) {
  289. u32 opmode = bfin_read_EMAC_OPMODE();
  290. new_state = 1;
  291. if (phydev->duplex)
  292. opmode |= FDMODE;
  293. else
  294. opmode &= ~(FDMODE);
  295. bfin_write_EMAC_OPMODE(opmode);
  296. lp->old_duplex = phydev->duplex;
  297. }
  298. if (phydev->speed != lp->old_speed) {
  299. #if defined(CONFIG_BFIN_MAC_RMII)
  300. u32 opmode = bfin_read_EMAC_OPMODE();
  301. switch (phydev->speed) {
  302. case 10:
  303. opmode |= RMII_10;
  304. break;
  305. case 100:
  306. opmode &= ~(RMII_10);
  307. break;
  308. default:
  309. printk(KERN_WARNING
  310. "%s: Ack! Speed (%d) is not 10/100!\n",
  311. DRV_NAME, phydev->speed);
  312. break;
  313. }
  314. bfin_write_EMAC_OPMODE(opmode);
  315. #endif
  316. new_state = 1;
  317. lp->old_speed = phydev->speed;
  318. }
  319. if (!lp->old_link) {
  320. new_state = 1;
  321. lp->old_link = 1;
  322. }
  323. } else if (lp->old_link) {
  324. new_state = 1;
  325. lp->old_link = 0;
  326. lp->old_speed = 0;
  327. lp->old_duplex = -1;
  328. }
  329. if (new_state) {
  330. u32 opmode = bfin_read_EMAC_OPMODE();
  331. phy_print_status(phydev);
  332. pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
  333. }
  334. spin_unlock_irqrestore(&lp->lock, flags);
  335. }
  336. /* MDC = 2.5 MHz */
  337. #define MDC_CLK 2500000
  338. static int mii_probe(struct net_device *dev)
  339. {
  340. struct bfin_mac_local *lp = netdev_priv(dev);
  341. struct phy_device *phydev = NULL;
  342. unsigned short sysctl;
  343. int i;
  344. u32 sclk, mdc_div;
  345. /* Enable PHY output early */
  346. if (!(bfin_read_VR_CTL() & CLKBUFOE))
  347. bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
  348. sclk = get_sclk();
  349. mdc_div = ((sclk / MDC_CLK) / 2) - 1;
  350. sysctl = bfin_read_EMAC_SYSCTL();
  351. sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
  352. bfin_write_EMAC_SYSCTL(sysctl);
  353. /* search for connect PHY device */
  354. for (i = 0; i < PHY_MAX_ADDR; i++) {
  355. struct phy_device *const tmp_phydev = lp->mii_bus->phy_map[i];
  356. if (!tmp_phydev)
  357. continue; /* no PHY here... */
  358. phydev = tmp_phydev;
  359. break; /* found it */
  360. }
  361. /* now we are supposed to have a proper phydev, to attach to... */
  362. if (!phydev) {
  363. printk(KERN_INFO "%s: Don't found any phy device at all\n",
  364. dev->name);
  365. return -ENODEV;
  366. }
  367. #if defined(CONFIG_BFIN_MAC_RMII)
  368. phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
  369. 0, PHY_INTERFACE_MODE_RMII);
  370. #else
  371. phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
  372. 0, PHY_INTERFACE_MODE_MII);
  373. #endif
  374. if (IS_ERR(phydev)) {
  375. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  376. return PTR_ERR(phydev);
  377. }
  378. /* mask with MAC supported features */
  379. phydev->supported &= (SUPPORTED_10baseT_Half
  380. | SUPPORTED_10baseT_Full
  381. | SUPPORTED_100baseT_Half
  382. | SUPPORTED_100baseT_Full
  383. | SUPPORTED_Autoneg
  384. | SUPPORTED_Pause | SUPPORTED_Asym_Pause
  385. | SUPPORTED_MII
  386. | SUPPORTED_TP);
  387. phydev->advertising = phydev->supported;
  388. lp->old_link = 0;
  389. lp->old_speed = 0;
  390. lp->old_duplex = -1;
  391. lp->phydev = phydev;
  392. printk(KERN_INFO "%s: attached PHY driver [%s] "
  393. "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
  394. "@sclk=%dMHz)\n",
  395. DRV_NAME, phydev->drv->name, dev_name(&phydev->dev), phydev->irq,
  396. MDC_CLK, mdc_div, sclk/1000000);
  397. return 0;
  398. }
  399. /*
  400. * Ethtool support
  401. */
  402. /*
  403. * interrupt routine for magic packet wakeup
  404. */
  405. static irqreturn_t bfin_mac_wake_interrupt(int irq, void *dev_id)
  406. {
  407. return IRQ_HANDLED;
  408. }
  409. static int
  410. bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  411. {
  412. struct bfin_mac_local *lp = netdev_priv(dev);
  413. if (lp->phydev)
  414. return phy_ethtool_gset(lp->phydev, cmd);
  415. return -EINVAL;
  416. }
  417. static int
  418. bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  419. {
  420. struct bfin_mac_local *lp = netdev_priv(dev);
  421. if (!capable(CAP_NET_ADMIN))
  422. return -EPERM;
  423. if (lp->phydev)
  424. return phy_ethtool_sset(lp->phydev, cmd);
  425. return -EINVAL;
  426. }
  427. static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
  428. struct ethtool_drvinfo *info)
  429. {
  430. strcpy(info->driver, DRV_NAME);
  431. strcpy(info->version, DRV_VERSION);
  432. strcpy(info->fw_version, "N/A");
  433. strcpy(info->bus_info, dev_name(&dev->dev));
  434. }
  435. static void bfin_mac_ethtool_getwol(struct net_device *dev,
  436. struct ethtool_wolinfo *wolinfo)
  437. {
  438. struct bfin_mac_local *lp = netdev_priv(dev);
  439. wolinfo->supported = WAKE_MAGIC;
  440. wolinfo->wolopts = lp->wol;
  441. }
  442. static int bfin_mac_ethtool_setwol(struct net_device *dev,
  443. struct ethtool_wolinfo *wolinfo)
  444. {
  445. struct bfin_mac_local *lp = netdev_priv(dev);
  446. int rc;
  447. if (wolinfo->wolopts & (WAKE_MAGICSECURE |
  448. WAKE_UCAST |
  449. WAKE_MCAST |
  450. WAKE_BCAST |
  451. WAKE_ARP))
  452. return -EOPNOTSUPP;
  453. lp->wol = wolinfo->wolopts;
  454. if (lp->wol && !lp->irq_wake_requested) {
  455. /* register wake irq handler */
  456. rc = request_irq(IRQ_MAC_WAKEDET, bfin_mac_wake_interrupt,
  457. IRQF_DISABLED, "EMAC_WAKE", dev);
  458. if (rc)
  459. return rc;
  460. lp->irq_wake_requested = true;
  461. }
  462. if (!lp->wol && lp->irq_wake_requested) {
  463. free_irq(IRQ_MAC_WAKEDET, dev);
  464. lp->irq_wake_requested = false;
  465. }
  466. /* Make sure the PHY driver doesn't suspend */
  467. device_init_wakeup(&dev->dev, lp->wol);
  468. return 0;
  469. }
  470. static const struct ethtool_ops bfin_mac_ethtool_ops = {
  471. .get_settings = bfin_mac_ethtool_getsettings,
  472. .set_settings = bfin_mac_ethtool_setsettings,
  473. .get_link = ethtool_op_get_link,
  474. .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
  475. .get_wol = bfin_mac_ethtool_getwol,
  476. .set_wol = bfin_mac_ethtool_setwol,
  477. };
  478. /**************************************************************************/
  479. void setup_system_regs(struct net_device *dev)
  480. {
  481. unsigned short sysctl;
  482. /*
  483. * Odd word alignment for Receive Frame DMA word
  484. * Configure checksum support and rcve frame word alignment
  485. */
  486. sysctl = bfin_read_EMAC_SYSCTL();
  487. sysctl |= RXDWA;
  488. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  489. sysctl |= RXCKS;
  490. #else
  491. sysctl &= ~RXCKS;
  492. #endif
  493. bfin_write_EMAC_SYSCTL(sysctl);
  494. bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
  495. /* Initialize the TX DMA channel registers */
  496. bfin_write_DMA2_X_COUNT(0);
  497. bfin_write_DMA2_X_MODIFY(4);
  498. bfin_write_DMA2_Y_COUNT(0);
  499. bfin_write_DMA2_Y_MODIFY(0);
  500. /* Initialize the RX DMA channel registers */
  501. bfin_write_DMA1_X_COUNT(0);
  502. bfin_write_DMA1_X_MODIFY(4);
  503. bfin_write_DMA1_Y_COUNT(0);
  504. bfin_write_DMA1_Y_MODIFY(0);
  505. }
  506. static void setup_mac_addr(u8 *mac_addr)
  507. {
  508. u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
  509. u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
  510. /* this depends on a little-endian machine */
  511. bfin_write_EMAC_ADDRLO(addr_low);
  512. bfin_write_EMAC_ADDRHI(addr_hi);
  513. }
  514. static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
  515. {
  516. struct sockaddr *addr = p;
  517. if (netif_running(dev))
  518. return -EBUSY;
  519. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  520. setup_mac_addr(dev->dev_addr);
  521. return 0;
  522. }
  523. #ifdef CONFIG_BFIN_MAC_USE_HWSTAMP
  524. #define bfin_mac_hwtstamp_is_none(cfg) ((cfg) == HWTSTAMP_FILTER_NONE)
  525. static int bfin_mac_hwtstamp_ioctl(struct net_device *netdev,
  526. struct ifreq *ifr, int cmd)
  527. {
  528. struct hwtstamp_config config;
  529. struct bfin_mac_local *lp = netdev_priv(netdev);
  530. u16 ptpctl;
  531. u32 ptpfv1, ptpfv2, ptpfv3, ptpfoff;
  532. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  533. return -EFAULT;
  534. pr_debug("%s config flag:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  535. __func__, config.flags, config.tx_type, config.rx_filter);
  536. /* reserved for future extensions */
  537. if (config.flags)
  538. return -EINVAL;
  539. if ((config.tx_type != HWTSTAMP_TX_OFF) &&
  540. (config.tx_type != HWTSTAMP_TX_ON))
  541. return -ERANGE;
  542. ptpctl = bfin_read_EMAC_PTP_CTL();
  543. switch (config.rx_filter) {
  544. case HWTSTAMP_FILTER_NONE:
  545. /*
  546. * Dont allow any timestamping
  547. */
  548. ptpfv3 = 0xFFFFFFFF;
  549. bfin_write_EMAC_PTP_FV3(ptpfv3);
  550. break;
  551. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  552. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  553. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  554. /*
  555. * Clear the five comparison mask bits (bits[12:8]) in EMAC_PTP_CTL)
  556. * to enable all the field matches.
  557. */
  558. ptpctl &= ~0x1F00;
  559. bfin_write_EMAC_PTP_CTL(ptpctl);
  560. /*
  561. * Keep the default values of the EMAC_PTP_FOFF register.
  562. */
  563. ptpfoff = 0x4A24170C;
  564. bfin_write_EMAC_PTP_FOFF(ptpfoff);
  565. /*
  566. * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
  567. * registers.
  568. */
  569. ptpfv1 = 0x11040800;
  570. bfin_write_EMAC_PTP_FV1(ptpfv1);
  571. ptpfv2 = 0x0140013F;
  572. bfin_write_EMAC_PTP_FV2(ptpfv2);
  573. /*
  574. * The default value (0xFFFC) allows the timestamping of both
  575. * received Sync messages and Delay_Req messages.
  576. */
  577. ptpfv3 = 0xFFFFFFFC;
  578. bfin_write_EMAC_PTP_FV3(ptpfv3);
  579. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  580. break;
  581. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  582. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  583. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  584. /* Clear all five comparison mask bits (bits[12:8]) in the
  585. * EMAC_PTP_CTL register to enable all the field matches.
  586. */
  587. ptpctl &= ~0x1F00;
  588. bfin_write_EMAC_PTP_CTL(ptpctl);
  589. /*
  590. * Keep the default values of the EMAC_PTP_FOFF register, except set
  591. * the PTPCOF field to 0x2A.
  592. */
  593. ptpfoff = 0x2A24170C;
  594. bfin_write_EMAC_PTP_FOFF(ptpfoff);
  595. /*
  596. * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
  597. * registers.
  598. */
  599. ptpfv1 = 0x11040800;
  600. bfin_write_EMAC_PTP_FV1(ptpfv1);
  601. ptpfv2 = 0x0140013F;
  602. bfin_write_EMAC_PTP_FV2(ptpfv2);
  603. /*
  604. * To allow the timestamping of Pdelay_Req and Pdelay_Resp, set
  605. * the value to 0xFFF0.
  606. */
  607. ptpfv3 = 0xFFFFFFF0;
  608. bfin_write_EMAC_PTP_FV3(ptpfv3);
  609. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  610. break;
  611. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  612. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  613. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  614. /*
  615. * Clear bits 8 and 12 of the EMAC_PTP_CTL register to enable only the
  616. * EFTM and PTPCM field comparison.
  617. */
  618. ptpctl &= ~0x1100;
  619. bfin_write_EMAC_PTP_CTL(ptpctl);
  620. /*
  621. * Keep the default values of all the fields of the EMAC_PTP_FOFF
  622. * register, except set the PTPCOF field to 0x0E.
  623. */
  624. ptpfoff = 0x0E24170C;
  625. bfin_write_EMAC_PTP_FOFF(ptpfoff);
  626. /*
  627. * Program bits [15:0] of the EMAC_PTP_FV1 register to 0x88F7, which
  628. * corresponds to PTP messages on the MAC layer.
  629. */
  630. ptpfv1 = 0x110488F7;
  631. bfin_write_EMAC_PTP_FV1(ptpfv1);
  632. ptpfv2 = 0x0140013F;
  633. bfin_write_EMAC_PTP_FV2(ptpfv2);
  634. /*
  635. * To allow the timestamping of Pdelay_Req and Pdelay_Resp
  636. * messages, set the value to 0xFFF0.
  637. */
  638. ptpfv3 = 0xFFFFFFF0;
  639. bfin_write_EMAC_PTP_FV3(ptpfv3);
  640. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  641. break;
  642. default:
  643. return -ERANGE;
  644. }
  645. if (config.tx_type == HWTSTAMP_TX_OFF &&
  646. bfin_mac_hwtstamp_is_none(config.rx_filter)) {
  647. ptpctl &= ~PTP_EN;
  648. bfin_write_EMAC_PTP_CTL(ptpctl);
  649. SSYNC();
  650. } else {
  651. ptpctl |= PTP_EN;
  652. bfin_write_EMAC_PTP_CTL(ptpctl);
  653. /*
  654. * clear any existing timestamp
  655. */
  656. bfin_read_EMAC_PTP_RXSNAPLO();
  657. bfin_read_EMAC_PTP_RXSNAPHI();
  658. bfin_read_EMAC_PTP_TXSNAPLO();
  659. bfin_read_EMAC_PTP_TXSNAPHI();
  660. /*
  661. * Set registers so that rollover occurs soon to test this.
  662. */
  663. bfin_write_EMAC_PTP_TIMELO(0x00000000);
  664. bfin_write_EMAC_PTP_TIMEHI(0xFF800000);
  665. SSYNC();
  666. lp->compare.last_update = 0;
  667. timecounter_init(&lp->clock,
  668. &lp->cycles,
  669. ktime_to_ns(ktime_get_real()));
  670. timecompare_update(&lp->compare, 0);
  671. }
  672. lp->stamp_cfg = config;
  673. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  674. -EFAULT : 0;
  675. }
  676. static void bfin_dump_hwtamp(char *s, ktime_t *hw, ktime_t *ts, struct timecompare *cmp)
  677. {
  678. ktime_t sys = ktime_get_real();
  679. pr_debug("%s %s hardware:%d,%d transform system:%d,%d system:%d,%d, cmp:%lld, %lld\n",
  680. __func__, s, hw->tv.sec, hw->tv.nsec, ts->tv.sec, ts->tv.nsec, sys.tv.sec,
  681. sys.tv.nsec, cmp->offset, cmp->skew);
  682. }
  683. static void bfin_tx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
  684. {
  685. struct bfin_mac_local *lp = netdev_priv(netdev);
  686. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
  687. int timeout_cnt = MAX_TIMEOUT_CNT;
  688. /* When doing time stamping, keep the connection to the socket
  689. * a while longer
  690. */
  691. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  692. /*
  693. * The timestamping is done at the EMAC module's MII/RMII interface
  694. * when the module sees the Start of Frame of an event message packet. This
  695. * interface is the closest possible place to the physical Ethernet transmission
  696. * medium, providing the best timing accuracy.
  697. */
  698. while ((!(bfin_read_EMAC_PTP_ISTAT() & TXTL)) && (--timeout_cnt))
  699. udelay(1);
  700. if (timeout_cnt == 0)
  701. printk(KERN_ERR DRV_NAME
  702. ": fails to timestamp the TX packet\n");
  703. else {
  704. struct skb_shared_hwtstamps shhwtstamps;
  705. u64 ns;
  706. u64 regval;
  707. regval = bfin_read_EMAC_PTP_TXSNAPLO();
  708. regval |= (u64)bfin_read_EMAC_PTP_TXSNAPHI() << 32;
  709. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  710. ns = timecounter_cyc2time(&lp->clock,
  711. regval);
  712. timecompare_update(&lp->compare, ns);
  713. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  714. shhwtstamps.syststamp =
  715. timecompare_transform(&lp->compare, ns);
  716. skb_tstamp_tx(skb, &shhwtstamps);
  717. bfin_dump_hwtamp("TX", &shhwtstamps.hwtstamp, &shhwtstamps.syststamp, &lp->compare);
  718. }
  719. }
  720. }
  721. static void bfin_rx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
  722. {
  723. struct bfin_mac_local *lp = netdev_priv(netdev);
  724. u32 valid;
  725. u64 regval, ns;
  726. struct skb_shared_hwtstamps *shhwtstamps;
  727. if (bfin_mac_hwtstamp_is_none(lp->stamp_cfg.rx_filter))
  728. return;
  729. valid = bfin_read_EMAC_PTP_ISTAT() & RXEL;
  730. if (!valid)
  731. return;
  732. shhwtstamps = skb_hwtstamps(skb);
  733. regval = bfin_read_EMAC_PTP_RXSNAPLO();
  734. regval |= (u64)bfin_read_EMAC_PTP_RXSNAPHI() << 32;
  735. ns = timecounter_cyc2time(&lp->clock, regval);
  736. timecompare_update(&lp->compare, ns);
  737. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  738. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  739. shhwtstamps->syststamp = timecompare_transform(&lp->compare, ns);
  740. bfin_dump_hwtamp("RX", &shhwtstamps->hwtstamp, &shhwtstamps->syststamp, &lp->compare);
  741. }
  742. /*
  743. * bfin_read_clock - read raw cycle counter (to be used by time counter)
  744. */
  745. static cycle_t bfin_read_clock(const struct cyclecounter *tc)
  746. {
  747. u64 stamp;
  748. stamp = bfin_read_EMAC_PTP_TIMELO();
  749. stamp |= (u64)bfin_read_EMAC_PTP_TIMEHI() << 32ULL;
  750. return stamp;
  751. }
  752. #define PTP_CLK 25000000
  753. static void bfin_mac_hwtstamp_init(struct net_device *netdev)
  754. {
  755. struct bfin_mac_local *lp = netdev_priv(netdev);
  756. u64 append;
  757. /* Initialize hardware timer */
  758. append = PTP_CLK * (1ULL << 32);
  759. do_div(append, get_sclk());
  760. bfin_write_EMAC_PTP_ADDEND((u32)append);
  761. memset(&lp->cycles, 0, sizeof(lp->cycles));
  762. lp->cycles.read = bfin_read_clock;
  763. lp->cycles.mask = CLOCKSOURCE_MASK(64);
  764. lp->cycles.mult = 1000000000 / PTP_CLK;
  765. lp->cycles.shift = 0;
  766. /* Synchronize our NIC clock against system wall clock */
  767. memset(&lp->compare, 0, sizeof(lp->compare));
  768. lp->compare.source = &lp->clock;
  769. lp->compare.target = ktime_get_real;
  770. lp->compare.num_samples = 10;
  771. /* Initialize hwstamp config */
  772. lp->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
  773. lp->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
  774. }
  775. #else
  776. # define bfin_mac_hwtstamp_is_none(cfg) 0
  777. # define bfin_mac_hwtstamp_init(dev)
  778. # define bfin_mac_hwtstamp_ioctl(dev, ifr, cmd) (-EOPNOTSUPP)
  779. # define bfin_rx_hwtstamp(dev, skb)
  780. # define bfin_tx_hwtstamp(dev, skb)
  781. #endif
  782. static inline void _tx_reclaim_skb(void)
  783. {
  784. do {
  785. tx_list_head->desc_a.config &= ~DMAEN;
  786. tx_list_head->status.status_word = 0;
  787. if (tx_list_head->skb) {
  788. dev_kfree_skb(tx_list_head->skb);
  789. tx_list_head->skb = NULL;
  790. }
  791. tx_list_head = tx_list_head->next;
  792. } while (tx_list_head->status.status_word != 0);
  793. }
  794. static void tx_reclaim_skb(struct bfin_mac_local *lp)
  795. {
  796. int timeout_cnt = MAX_TIMEOUT_CNT;
  797. if (tx_list_head->status.status_word != 0)
  798. _tx_reclaim_skb();
  799. if (current_tx_ptr->next == tx_list_head) {
  800. while (tx_list_head->status.status_word == 0) {
  801. /* slow down polling to avoid too many queue stop. */
  802. udelay(10);
  803. /* reclaim skb if DMA is not running. */
  804. if (!(bfin_read_DMA2_IRQ_STATUS() & DMA_RUN))
  805. break;
  806. if (timeout_cnt-- < 0)
  807. break;
  808. }
  809. if (timeout_cnt >= 0)
  810. _tx_reclaim_skb();
  811. else
  812. netif_stop_queue(lp->ndev);
  813. }
  814. if (current_tx_ptr->next != tx_list_head &&
  815. netif_queue_stopped(lp->ndev))
  816. netif_wake_queue(lp->ndev);
  817. if (tx_list_head != current_tx_ptr) {
  818. /* shorten the timer interval if tx queue is stopped */
  819. if (netif_queue_stopped(lp->ndev))
  820. lp->tx_reclaim_timer.expires =
  821. jiffies + (TX_RECLAIM_JIFFIES >> 4);
  822. else
  823. lp->tx_reclaim_timer.expires =
  824. jiffies + TX_RECLAIM_JIFFIES;
  825. mod_timer(&lp->tx_reclaim_timer,
  826. lp->tx_reclaim_timer.expires);
  827. }
  828. return;
  829. }
  830. static void tx_reclaim_skb_timeout(unsigned long lp)
  831. {
  832. tx_reclaim_skb((struct bfin_mac_local *)lp);
  833. }
  834. static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
  835. struct net_device *dev)
  836. {
  837. struct bfin_mac_local *lp = netdev_priv(dev);
  838. u16 *data;
  839. u32 data_align = (unsigned long)(skb->data) & 0x3;
  840. current_tx_ptr->skb = skb;
  841. if (data_align == 0x2) {
  842. /* move skb->data to current_tx_ptr payload */
  843. data = (u16 *)(skb->data) - 1;
  844. *data = (u16)(skb->len);
  845. /*
  846. * When transmitting an Ethernet packet, the PTP_TSYNC module requires
  847. * a DMA_Length_Word field associated with the packet. The lower 12 bits
  848. * of this field are the length of the packet payload in bytes and the higher
  849. * 4 bits are the timestamping enable field.
  850. */
  851. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
  852. *data |= 0x1000;
  853. current_tx_ptr->desc_a.start_addr = (u32)data;
  854. /* this is important! */
  855. blackfin_dcache_flush_range((u32)data,
  856. (u32)((u8 *)data + skb->len + 4));
  857. } else {
  858. *((u16 *)(current_tx_ptr->packet)) = (u16)(skb->len);
  859. /* enable timestamping for the sent packet */
  860. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
  861. *((u16 *)(current_tx_ptr->packet)) |= 0x1000;
  862. memcpy((u8 *)(current_tx_ptr->packet + 2), skb->data,
  863. skb->len);
  864. current_tx_ptr->desc_a.start_addr =
  865. (u32)current_tx_ptr->packet;
  866. blackfin_dcache_flush_range(
  867. (u32)current_tx_ptr->packet,
  868. (u32)(current_tx_ptr->packet + skb->len + 2));
  869. }
  870. /* make sure the internal data buffers in the core are drained
  871. * so that the DMA descriptors are completely written when the
  872. * DMA engine goes to fetch them below
  873. */
  874. SSYNC();
  875. /* always clear status buffer before start tx dma */
  876. current_tx_ptr->status.status_word = 0;
  877. /* enable this packet's dma */
  878. current_tx_ptr->desc_a.config |= DMAEN;
  879. /* tx dma is running, just return */
  880. if (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)
  881. goto out;
  882. /* tx dma is not running */
  883. bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
  884. /* dma enabled, read from memory, size is 6 */
  885. bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
  886. /* Turn on the EMAC tx */
  887. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  888. out:
  889. bfin_tx_hwtstamp(dev, skb);
  890. current_tx_ptr = current_tx_ptr->next;
  891. dev->stats.tx_packets++;
  892. dev->stats.tx_bytes += (skb->len);
  893. tx_reclaim_skb(lp);
  894. return NETDEV_TX_OK;
  895. }
  896. #define IP_HEADER_OFF 0
  897. #define RX_ERROR_MASK (RX_LONG | RX_ALIGN | RX_CRC | RX_LEN | \
  898. RX_FRAG | RX_ADDR | RX_DMAO | RX_PHY | RX_LATE | RX_RANGE)
  899. static void bfin_mac_rx(struct net_device *dev)
  900. {
  901. struct sk_buff *skb, *new_skb;
  902. unsigned short len;
  903. struct bfin_mac_local *lp __maybe_unused = netdev_priv(dev);
  904. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  905. unsigned int i;
  906. unsigned char fcs[ETH_FCS_LEN + 1];
  907. #endif
  908. /* check if frame status word reports an error condition
  909. * we which case we simply drop the packet
  910. */
  911. if (current_rx_ptr->status.status_word & RX_ERROR_MASK) {
  912. printk(KERN_NOTICE DRV_NAME
  913. ": rx: receive error - packet dropped\n");
  914. dev->stats.rx_dropped++;
  915. goto out;
  916. }
  917. /* allocate a new skb for next time receive */
  918. skb = current_rx_ptr->skb;
  919. new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
  920. if (!new_skb) {
  921. printk(KERN_NOTICE DRV_NAME
  922. ": rx: low on mem - packet dropped\n");
  923. dev->stats.rx_dropped++;
  924. goto out;
  925. }
  926. /* reserve 2 bytes for RXDWA padding */
  927. skb_reserve(new_skb, NET_IP_ALIGN);
  928. /* Invidate the data cache of skb->data range when it is write back
  929. * cache. It will prevent overwritting the new data from DMA
  930. */
  931. blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
  932. (unsigned long)new_skb->end);
  933. current_rx_ptr->skb = new_skb;
  934. current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
  935. len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
  936. /* Deduce Ethernet FCS length from Ethernet payload length */
  937. len -= ETH_FCS_LEN;
  938. skb_put(skb, len);
  939. skb->protocol = eth_type_trans(skb, dev);
  940. bfin_rx_hwtstamp(dev, skb);
  941. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  942. /* Checksum offloading only works for IPv4 packets with the standard IP header
  943. * length of 20 bytes, because the blackfin MAC checksum calculation is
  944. * based on that assumption. We must NOT use the calculated checksum if our
  945. * IP version or header break that assumption.
  946. */
  947. if (skb->data[IP_HEADER_OFF] == 0x45) {
  948. skb->csum = current_rx_ptr->status.ip_payload_csum;
  949. /*
  950. * Deduce Ethernet FCS from hardware generated IP payload checksum.
  951. * IP checksum is based on 16-bit one's complement algorithm.
  952. * To deduce a value from checksum is equal to add its inversion.
  953. * If the IP payload len is odd, the inversed FCS should also
  954. * begin from odd address and leave first byte zero.
  955. */
  956. if (skb->len % 2) {
  957. fcs[0] = 0;
  958. for (i = 0; i < ETH_FCS_LEN; i++)
  959. fcs[i + 1] = ~skb->data[skb->len + i];
  960. skb->csum = csum_partial(fcs, ETH_FCS_LEN + 1, skb->csum);
  961. } else {
  962. for (i = 0; i < ETH_FCS_LEN; i++)
  963. fcs[i] = ~skb->data[skb->len + i];
  964. skb->csum = csum_partial(fcs, ETH_FCS_LEN, skb->csum);
  965. }
  966. skb->ip_summed = CHECKSUM_COMPLETE;
  967. }
  968. #endif
  969. netif_rx(skb);
  970. dev->stats.rx_packets++;
  971. dev->stats.rx_bytes += len;
  972. out:
  973. current_rx_ptr->status.status_word = 0x00000000;
  974. current_rx_ptr = current_rx_ptr->next;
  975. }
  976. /* interrupt routine to handle rx and error signal */
  977. static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
  978. {
  979. struct net_device *dev = dev_id;
  980. int number = 0;
  981. get_one_packet:
  982. if (current_rx_ptr->status.status_word == 0) {
  983. /* no more new packet received */
  984. if (number == 0) {
  985. if (current_rx_ptr->next->status.status_word != 0) {
  986. current_rx_ptr = current_rx_ptr->next;
  987. goto real_rx;
  988. }
  989. }
  990. bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
  991. DMA_DONE | DMA_ERR);
  992. return IRQ_HANDLED;
  993. }
  994. real_rx:
  995. bfin_mac_rx(dev);
  996. number++;
  997. goto get_one_packet;
  998. }
  999. #ifdef CONFIG_NET_POLL_CONTROLLER
  1000. static void bfin_mac_poll(struct net_device *dev)
  1001. {
  1002. struct bfin_mac_local *lp = netdev_priv(dev);
  1003. disable_irq(IRQ_MAC_RX);
  1004. bfin_mac_interrupt(IRQ_MAC_RX, dev);
  1005. tx_reclaim_skb(lp);
  1006. enable_irq(IRQ_MAC_RX);
  1007. }
  1008. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1009. static void bfin_mac_disable(void)
  1010. {
  1011. unsigned int opmode;
  1012. opmode = bfin_read_EMAC_OPMODE();
  1013. opmode &= (~RE);
  1014. opmode &= (~TE);
  1015. /* Turn off the EMAC */
  1016. bfin_write_EMAC_OPMODE(opmode);
  1017. }
  1018. /*
  1019. * Enable Interrupts, Receive, and Transmit
  1020. */
  1021. static int bfin_mac_enable(void)
  1022. {
  1023. int ret;
  1024. u32 opmode;
  1025. pr_debug("%s: %s\n", DRV_NAME, __func__);
  1026. /* Set RX DMA */
  1027. bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
  1028. bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
  1029. /* Wait MII done */
  1030. ret = bfin_mdio_poll();
  1031. if (ret)
  1032. return ret;
  1033. /* We enable only RX here */
  1034. /* ASTP : Enable Automatic Pad Stripping
  1035. PR : Promiscuous Mode for test
  1036. PSF : Receive frames with total length less than 64 bytes.
  1037. FDMODE : Full Duplex Mode
  1038. LB : Internal Loopback for test
  1039. RE : Receiver Enable */
  1040. opmode = bfin_read_EMAC_OPMODE();
  1041. if (opmode & FDMODE)
  1042. opmode |= PSF;
  1043. else
  1044. opmode |= DRO | DC | PSF;
  1045. opmode |= RE;
  1046. #if defined(CONFIG_BFIN_MAC_RMII)
  1047. opmode |= RMII; /* For Now only 100MBit are supported */
  1048. #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
  1049. opmode |= TE;
  1050. #endif
  1051. #endif
  1052. /* Turn on the EMAC rx */
  1053. bfin_write_EMAC_OPMODE(opmode);
  1054. return 0;
  1055. }
  1056. /* Our watchdog timed out. Called by the networking layer */
  1057. static void bfin_mac_timeout(struct net_device *dev)
  1058. {
  1059. struct bfin_mac_local *lp = netdev_priv(dev);
  1060. pr_debug("%s: %s\n", dev->name, __func__);
  1061. bfin_mac_disable();
  1062. del_timer(&lp->tx_reclaim_timer);
  1063. /* reset tx queue and free skb */
  1064. while (tx_list_head != current_tx_ptr) {
  1065. tx_list_head->desc_a.config &= ~DMAEN;
  1066. tx_list_head->status.status_word = 0;
  1067. if (tx_list_head->skb) {
  1068. dev_kfree_skb(tx_list_head->skb);
  1069. tx_list_head->skb = NULL;
  1070. }
  1071. tx_list_head = tx_list_head->next;
  1072. }
  1073. if (netif_queue_stopped(lp->ndev))
  1074. netif_wake_queue(lp->ndev);
  1075. bfin_mac_enable();
  1076. /* We can accept TX packets again */
  1077. dev->trans_start = jiffies; /* prevent tx timeout */
  1078. netif_wake_queue(dev);
  1079. }
  1080. static void bfin_mac_multicast_hash(struct net_device *dev)
  1081. {
  1082. u32 emac_hashhi, emac_hashlo;
  1083. struct netdev_hw_addr *ha;
  1084. char *addrs;
  1085. u32 crc;
  1086. emac_hashhi = emac_hashlo = 0;
  1087. netdev_for_each_mc_addr(ha, dev) {
  1088. addrs = ha->addr;
  1089. /* skip non-multicast addresses */
  1090. if (!(*addrs & 1))
  1091. continue;
  1092. crc = ether_crc(ETH_ALEN, addrs);
  1093. crc >>= 26;
  1094. if (crc & 0x20)
  1095. emac_hashhi |= 1 << (crc & 0x1f);
  1096. else
  1097. emac_hashlo |= 1 << (crc & 0x1f);
  1098. }
  1099. bfin_write_EMAC_HASHHI(emac_hashhi);
  1100. bfin_write_EMAC_HASHLO(emac_hashlo);
  1101. }
  1102. /*
  1103. * This routine will, depending on the values passed to it,
  1104. * either make it accept multicast packets, go into
  1105. * promiscuous mode (for TCPDUMP and cousins) or accept
  1106. * a select set of multicast packets
  1107. */
  1108. static void bfin_mac_set_multicast_list(struct net_device *dev)
  1109. {
  1110. u32 sysctl;
  1111. if (dev->flags & IFF_PROMISC) {
  1112. printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
  1113. sysctl = bfin_read_EMAC_OPMODE();
  1114. sysctl |= PR;
  1115. bfin_write_EMAC_OPMODE(sysctl);
  1116. } else if (dev->flags & IFF_ALLMULTI) {
  1117. /* accept all multicast */
  1118. sysctl = bfin_read_EMAC_OPMODE();
  1119. sysctl |= PAM;
  1120. bfin_write_EMAC_OPMODE(sysctl);
  1121. } else if (!netdev_mc_empty(dev)) {
  1122. /* set up multicast hash table */
  1123. sysctl = bfin_read_EMAC_OPMODE();
  1124. sysctl |= HM;
  1125. bfin_write_EMAC_OPMODE(sysctl);
  1126. bfin_mac_multicast_hash(dev);
  1127. } else {
  1128. /* clear promisc or multicast mode */
  1129. sysctl = bfin_read_EMAC_OPMODE();
  1130. sysctl &= ~(RAF | PAM);
  1131. bfin_write_EMAC_OPMODE(sysctl);
  1132. }
  1133. }
  1134. static int bfin_mac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1135. {
  1136. switch (cmd) {
  1137. case SIOCSHWTSTAMP:
  1138. return bfin_mac_hwtstamp_ioctl(netdev, ifr, cmd);
  1139. default:
  1140. return -EOPNOTSUPP;
  1141. }
  1142. }
  1143. /*
  1144. * this puts the device in an inactive state
  1145. */
  1146. static void bfin_mac_shutdown(struct net_device *dev)
  1147. {
  1148. /* Turn off the EMAC */
  1149. bfin_write_EMAC_OPMODE(0x00000000);
  1150. /* Turn off the EMAC RX DMA */
  1151. bfin_write_DMA1_CONFIG(0x0000);
  1152. bfin_write_DMA2_CONFIG(0x0000);
  1153. }
  1154. /*
  1155. * Open and Initialize the interface
  1156. *
  1157. * Set up everything, reset the card, etc..
  1158. */
  1159. static int bfin_mac_open(struct net_device *dev)
  1160. {
  1161. struct bfin_mac_local *lp = netdev_priv(dev);
  1162. int ret;
  1163. pr_debug("%s: %s\n", dev->name, __func__);
  1164. /*
  1165. * Check that the address is valid. If its not, refuse
  1166. * to bring the device up. The user must specify an
  1167. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  1168. */
  1169. if (!is_valid_ether_addr(dev->dev_addr)) {
  1170. printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
  1171. return -EINVAL;
  1172. }
  1173. /* initial rx and tx list */
  1174. ret = desc_list_init();
  1175. if (ret)
  1176. return ret;
  1177. phy_start(lp->phydev);
  1178. phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
  1179. setup_system_regs(dev);
  1180. setup_mac_addr(dev->dev_addr);
  1181. bfin_mac_disable();
  1182. ret = bfin_mac_enable();
  1183. if (ret)
  1184. return ret;
  1185. pr_debug("hardware init finished\n");
  1186. netif_start_queue(dev);
  1187. netif_carrier_on(dev);
  1188. return 0;
  1189. }
  1190. /*
  1191. * this makes the board clean up everything that it can
  1192. * and not talk to the outside world. Caused by
  1193. * an 'ifconfig ethX down'
  1194. */
  1195. static int bfin_mac_close(struct net_device *dev)
  1196. {
  1197. struct bfin_mac_local *lp = netdev_priv(dev);
  1198. pr_debug("%s: %s\n", dev->name, __func__);
  1199. netif_stop_queue(dev);
  1200. netif_carrier_off(dev);
  1201. phy_stop(lp->phydev);
  1202. phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
  1203. /* clear everything */
  1204. bfin_mac_shutdown(dev);
  1205. /* free the rx/tx buffers */
  1206. desc_list_free();
  1207. return 0;
  1208. }
  1209. static const struct net_device_ops bfin_mac_netdev_ops = {
  1210. .ndo_open = bfin_mac_open,
  1211. .ndo_stop = bfin_mac_close,
  1212. .ndo_start_xmit = bfin_mac_hard_start_xmit,
  1213. .ndo_set_mac_address = bfin_mac_set_mac_address,
  1214. .ndo_tx_timeout = bfin_mac_timeout,
  1215. .ndo_set_multicast_list = bfin_mac_set_multicast_list,
  1216. .ndo_do_ioctl = bfin_mac_ioctl,
  1217. .ndo_validate_addr = eth_validate_addr,
  1218. .ndo_change_mtu = eth_change_mtu,
  1219. #ifdef CONFIG_NET_POLL_CONTROLLER
  1220. .ndo_poll_controller = bfin_mac_poll,
  1221. #endif
  1222. };
  1223. static int __devinit bfin_mac_probe(struct platform_device *pdev)
  1224. {
  1225. struct net_device *ndev;
  1226. struct bfin_mac_local *lp;
  1227. struct platform_device *pd;
  1228. int rc;
  1229. ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
  1230. if (!ndev) {
  1231. dev_err(&pdev->dev, "Cannot allocate net device!\n");
  1232. return -ENOMEM;
  1233. }
  1234. SET_NETDEV_DEV(ndev, &pdev->dev);
  1235. platform_set_drvdata(pdev, ndev);
  1236. lp = netdev_priv(ndev);
  1237. lp->ndev = ndev;
  1238. /* Grab the MAC address in the MAC */
  1239. *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
  1240. *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
  1241. /* probe mac */
  1242. /*todo: how to proble? which is revision_register */
  1243. bfin_write_EMAC_ADDRLO(0x12345678);
  1244. if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
  1245. dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
  1246. rc = -ENODEV;
  1247. goto out_err_probe_mac;
  1248. }
  1249. /*
  1250. * Is it valid? (Did bootloader initialize it?)
  1251. * Grab the MAC from the board somehow
  1252. * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
  1253. */
  1254. if (!is_valid_ether_addr(ndev->dev_addr))
  1255. bfin_get_ether_addr(ndev->dev_addr);
  1256. /* If still not valid, get a random one */
  1257. if (!is_valid_ether_addr(ndev->dev_addr))
  1258. random_ether_addr(ndev->dev_addr);
  1259. setup_mac_addr(ndev->dev_addr);
  1260. if (!pdev->dev.platform_data) {
  1261. dev_err(&pdev->dev, "Cannot get platform device bfin_mii_bus!\n");
  1262. rc = -ENODEV;
  1263. goto out_err_probe_mac;
  1264. }
  1265. pd = pdev->dev.platform_data;
  1266. lp->mii_bus = platform_get_drvdata(pd);
  1267. if (!lp->mii_bus) {
  1268. dev_err(&pdev->dev, "Cannot get mii_bus!\n");
  1269. rc = -ENODEV;
  1270. goto out_err_mii_bus_probe;
  1271. }
  1272. lp->mii_bus->priv = ndev;
  1273. rc = mii_probe(ndev);
  1274. if (rc) {
  1275. dev_err(&pdev->dev, "MII Probe failed!\n");
  1276. goto out_err_mii_probe;
  1277. }
  1278. /* Fill in the fields of the device structure with ethernet values. */
  1279. ether_setup(ndev);
  1280. ndev->netdev_ops = &bfin_mac_netdev_ops;
  1281. ndev->ethtool_ops = &bfin_mac_ethtool_ops;
  1282. init_timer(&lp->tx_reclaim_timer);
  1283. lp->tx_reclaim_timer.data = (unsigned long)lp;
  1284. lp->tx_reclaim_timer.function = tx_reclaim_skb_timeout;
  1285. spin_lock_init(&lp->lock);
  1286. /* now, enable interrupts */
  1287. /* register irq handler */
  1288. rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
  1289. IRQF_DISABLED, "EMAC_RX", ndev);
  1290. if (rc) {
  1291. dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
  1292. rc = -EBUSY;
  1293. goto out_err_request_irq;
  1294. }
  1295. rc = register_netdev(ndev);
  1296. if (rc) {
  1297. dev_err(&pdev->dev, "Cannot register net device!\n");
  1298. goto out_err_reg_ndev;
  1299. }
  1300. bfin_mac_hwtstamp_init(ndev);
  1301. /* now, print out the card info, in a short format.. */
  1302. dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
  1303. return 0;
  1304. out_err_reg_ndev:
  1305. free_irq(IRQ_MAC_RX, ndev);
  1306. out_err_request_irq:
  1307. out_err_mii_probe:
  1308. mdiobus_unregister(lp->mii_bus);
  1309. mdiobus_free(lp->mii_bus);
  1310. out_err_mii_bus_probe:
  1311. peripheral_free_list(pin_req);
  1312. out_err_probe_mac:
  1313. platform_set_drvdata(pdev, NULL);
  1314. free_netdev(ndev);
  1315. return rc;
  1316. }
  1317. static int __devexit bfin_mac_remove(struct platform_device *pdev)
  1318. {
  1319. struct net_device *ndev = platform_get_drvdata(pdev);
  1320. struct bfin_mac_local *lp = netdev_priv(ndev);
  1321. platform_set_drvdata(pdev, NULL);
  1322. lp->mii_bus->priv = NULL;
  1323. unregister_netdev(ndev);
  1324. free_irq(IRQ_MAC_RX, ndev);
  1325. free_netdev(ndev);
  1326. peripheral_free_list(pin_req);
  1327. return 0;
  1328. }
  1329. #ifdef CONFIG_PM
  1330. static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
  1331. {
  1332. struct net_device *net_dev = platform_get_drvdata(pdev);
  1333. struct bfin_mac_local *lp = netdev_priv(net_dev);
  1334. if (lp->wol) {
  1335. bfin_write_EMAC_OPMODE((bfin_read_EMAC_OPMODE() & ~TE) | RE);
  1336. bfin_write_EMAC_WKUP_CTL(MPKE);
  1337. enable_irq_wake(IRQ_MAC_WAKEDET);
  1338. } else {
  1339. if (netif_running(net_dev))
  1340. bfin_mac_close(net_dev);
  1341. }
  1342. return 0;
  1343. }
  1344. static int bfin_mac_resume(struct platform_device *pdev)
  1345. {
  1346. struct net_device *net_dev = platform_get_drvdata(pdev);
  1347. struct bfin_mac_local *lp = netdev_priv(net_dev);
  1348. if (lp->wol) {
  1349. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  1350. bfin_write_EMAC_WKUP_CTL(0);
  1351. disable_irq_wake(IRQ_MAC_WAKEDET);
  1352. } else {
  1353. if (netif_running(net_dev))
  1354. bfin_mac_open(net_dev);
  1355. }
  1356. return 0;
  1357. }
  1358. #else
  1359. #define bfin_mac_suspend NULL
  1360. #define bfin_mac_resume NULL
  1361. #endif /* CONFIG_PM */
  1362. static int __devinit bfin_mii_bus_probe(struct platform_device *pdev)
  1363. {
  1364. struct mii_bus *miibus;
  1365. int rc, i;
  1366. /*
  1367. * We are setting up a network card,
  1368. * so set the GPIO pins to Ethernet mode
  1369. */
  1370. rc = peripheral_request_list(pin_req, DRV_NAME);
  1371. if (rc) {
  1372. dev_err(&pdev->dev, "Requesting peripherals failed!\n");
  1373. return rc;
  1374. }
  1375. rc = -ENOMEM;
  1376. miibus = mdiobus_alloc();
  1377. if (miibus == NULL)
  1378. goto out_err_alloc;
  1379. miibus->read = bfin_mdiobus_read;
  1380. miibus->write = bfin_mdiobus_write;
  1381. miibus->reset = bfin_mdiobus_reset;
  1382. miibus->parent = &pdev->dev;
  1383. miibus->name = "bfin_mii_bus";
  1384. snprintf(miibus->id, MII_BUS_ID_SIZE, "0");
  1385. miibus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  1386. if (miibus->irq == NULL)
  1387. goto out_err_alloc;
  1388. for (i = 0; i < PHY_MAX_ADDR; ++i)
  1389. miibus->irq[i] = PHY_POLL;
  1390. rc = mdiobus_register(miibus);
  1391. if (rc) {
  1392. dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
  1393. goto out_err_mdiobus_register;
  1394. }
  1395. platform_set_drvdata(pdev, miibus);
  1396. return 0;
  1397. out_err_mdiobus_register:
  1398. kfree(miibus->irq);
  1399. mdiobus_free(miibus);
  1400. out_err_alloc:
  1401. peripheral_free_list(pin_req);
  1402. return rc;
  1403. }
  1404. static int __devexit bfin_mii_bus_remove(struct platform_device *pdev)
  1405. {
  1406. struct mii_bus *miibus = platform_get_drvdata(pdev);
  1407. platform_set_drvdata(pdev, NULL);
  1408. mdiobus_unregister(miibus);
  1409. kfree(miibus->irq);
  1410. mdiobus_free(miibus);
  1411. peripheral_free_list(pin_req);
  1412. return 0;
  1413. }
  1414. static struct platform_driver bfin_mii_bus_driver = {
  1415. .probe = bfin_mii_bus_probe,
  1416. .remove = __devexit_p(bfin_mii_bus_remove),
  1417. .driver = {
  1418. .name = "bfin_mii_bus",
  1419. .owner = THIS_MODULE,
  1420. },
  1421. };
  1422. static struct platform_driver bfin_mac_driver = {
  1423. .probe = bfin_mac_probe,
  1424. .remove = __devexit_p(bfin_mac_remove),
  1425. .resume = bfin_mac_resume,
  1426. .suspend = bfin_mac_suspend,
  1427. .driver = {
  1428. .name = DRV_NAME,
  1429. .owner = THIS_MODULE,
  1430. },
  1431. };
  1432. static int __init bfin_mac_init(void)
  1433. {
  1434. int ret;
  1435. ret = platform_driver_register(&bfin_mii_bus_driver);
  1436. if (!ret)
  1437. return platform_driver_register(&bfin_mac_driver);
  1438. return -ENODEV;
  1439. }
  1440. module_init(bfin_mac_init);
  1441. static void __exit bfin_mac_cleanup(void)
  1442. {
  1443. platform_driver_unregister(&bfin_mac_driver);
  1444. platform_driver_unregister(&bfin_mii_bus_driver);
  1445. }
  1446. module_exit(bfin_mac_cleanup);