bfin_mac.c 44 KB

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