bfin_mac.c 44 KB

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