bfin_mac.c 45 KB

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