bfin_mac.c 39 KB

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