bfin_mac.c 46 KB

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