w90p910_ether.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. * Copyright (c) 2008-2009 Nuvoton technology corporation.
  3. *
  4. * Wan ZongShun <mcuos.com@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation;version 2 of the License.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/mii.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/clk.h>
  20. #include <linux/gfp.h>
  21. #define DRV_MODULE_NAME "w90p910-emc"
  22. #define DRV_MODULE_VERSION "0.1"
  23. /* Ethernet MAC Registers */
  24. #define REG_CAMCMR 0x00
  25. #define REG_CAMEN 0x04
  26. #define REG_CAMM_BASE 0x08
  27. #define REG_CAML_BASE 0x0c
  28. #define REG_TXDLSA 0x88
  29. #define REG_RXDLSA 0x8C
  30. #define REG_MCMDR 0x90
  31. #define REG_MIID 0x94
  32. #define REG_MIIDA 0x98
  33. #define REG_FFTCR 0x9C
  34. #define REG_TSDR 0xa0
  35. #define REG_RSDR 0xa4
  36. #define REG_DMARFC 0xa8
  37. #define REG_MIEN 0xac
  38. #define REG_MISTA 0xb0
  39. #define REG_CTXDSA 0xcc
  40. #define REG_CTXBSA 0xd0
  41. #define REG_CRXDSA 0xd4
  42. #define REG_CRXBSA 0xd8
  43. /* mac controller bit */
  44. #define MCMDR_RXON 0x01
  45. #define MCMDR_ACP (0x01 << 3)
  46. #define MCMDR_SPCRC (0x01 << 5)
  47. #define MCMDR_TXON (0x01 << 8)
  48. #define MCMDR_FDUP (0x01 << 18)
  49. #define MCMDR_ENMDC (0x01 << 19)
  50. #define MCMDR_OPMOD (0x01 << 20)
  51. #define SWR (0x01 << 24)
  52. /* cam command regiser */
  53. #define CAMCMR_AUP 0x01
  54. #define CAMCMR_AMP (0x01 << 1)
  55. #define CAMCMR_ABP (0x01 << 2)
  56. #define CAMCMR_CCAM (0x01 << 3)
  57. #define CAMCMR_ECMP (0x01 << 4)
  58. #define CAM0EN 0x01
  59. /* mac mii controller bit */
  60. #define MDCCR (0x0a << 20)
  61. #define PHYAD (0x01 << 8)
  62. #define PHYWR (0x01 << 16)
  63. #define PHYBUSY (0x01 << 17)
  64. #define PHYPRESP (0x01 << 18)
  65. #define CAM_ENTRY_SIZE 0x08
  66. /* rx and tx status */
  67. #define TXDS_TXCP (0x01 << 19)
  68. #define RXDS_CRCE (0x01 << 17)
  69. #define RXDS_PTLE (0x01 << 19)
  70. #define RXDS_RXGD (0x01 << 20)
  71. #define RXDS_ALIE (0x01 << 21)
  72. #define RXDS_RP (0x01 << 22)
  73. /* mac interrupt status*/
  74. #define MISTA_EXDEF (0x01 << 19)
  75. #define MISTA_TXBERR (0x01 << 24)
  76. #define MISTA_TDU (0x01 << 23)
  77. #define MISTA_RDU (0x01 << 10)
  78. #define MISTA_RXBERR (0x01 << 11)
  79. #define ENSTART 0x01
  80. #define ENRXINTR 0x01
  81. #define ENRXGD (0x01 << 4)
  82. #define ENRXBERR (0x01 << 11)
  83. #define ENTXINTR (0x01 << 16)
  84. #define ENTXCP (0x01 << 18)
  85. #define ENTXABT (0x01 << 21)
  86. #define ENTXBERR (0x01 << 24)
  87. #define ENMDC (0x01 << 19)
  88. #define PHYBUSY (0x01 << 17)
  89. #define MDCCR_VAL 0xa00000
  90. /* rx and tx owner bit */
  91. #define RX_OWEN_DMA (0x01 << 31)
  92. #define RX_OWEN_CPU (~(0x03 << 30))
  93. #define TX_OWEN_DMA (0x01 << 31)
  94. #define TX_OWEN_CPU (~(0x01 << 31))
  95. /* tx frame desc controller bit */
  96. #define MACTXINTEN 0x04
  97. #define CRCMODE 0x02
  98. #define PADDINGMODE 0x01
  99. /* fftcr controller bit */
  100. #define TXTHD (0x03 << 8)
  101. #define BLENGTH (0x01 << 20)
  102. /* global setting for driver */
  103. #define RX_DESC_SIZE 50
  104. #define TX_DESC_SIZE 10
  105. #define MAX_RBUFF_SZ 0x600
  106. #define MAX_TBUFF_SZ 0x600
  107. #define TX_TIMEOUT (HZ/2)
  108. #define DELAY 1000
  109. #define CAM0 0x0
  110. static int w90p910_mdio_read(struct net_device *dev, int phy_id, int reg);
  111. struct w90p910_rxbd {
  112. unsigned int sl;
  113. unsigned int buffer;
  114. unsigned int reserved;
  115. unsigned int next;
  116. };
  117. struct w90p910_txbd {
  118. unsigned int mode;
  119. unsigned int buffer;
  120. unsigned int sl;
  121. unsigned int next;
  122. };
  123. struct recv_pdesc {
  124. struct w90p910_rxbd desclist[RX_DESC_SIZE];
  125. char recv_buf[RX_DESC_SIZE][MAX_RBUFF_SZ];
  126. };
  127. struct tran_pdesc {
  128. struct w90p910_txbd desclist[TX_DESC_SIZE];
  129. char tran_buf[TX_DESC_SIZE][MAX_TBUFF_SZ];
  130. };
  131. struct w90p910_ether {
  132. struct recv_pdesc *rdesc;
  133. struct tran_pdesc *tdesc;
  134. dma_addr_t rdesc_phys;
  135. dma_addr_t tdesc_phys;
  136. struct net_device_stats stats;
  137. struct platform_device *pdev;
  138. struct resource *res;
  139. struct sk_buff *skb;
  140. struct clk *clk;
  141. struct clk *rmiiclk;
  142. struct mii_if_info mii;
  143. struct timer_list check_timer;
  144. void __iomem *reg;
  145. int rxirq;
  146. int txirq;
  147. unsigned int cur_tx;
  148. unsigned int cur_rx;
  149. unsigned int finish_tx;
  150. unsigned int rx_packets;
  151. unsigned int rx_bytes;
  152. unsigned int start_tx_ptr;
  153. unsigned int start_rx_ptr;
  154. unsigned int linkflag;
  155. };
  156. static void update_linkspeed_register(struct net_device *dev,
  157. unsigned int speed, unsigned int duplex)
  158. {
  159. struct w90p910_ether *ether = netdev_priv(dev);
  160. unsigned int val;
  161. val = __raw_readl(ether->reg + REG_MCMDR);
  162. if (speed == SPEED_100) {
  163. /* 100 full/half duplex */
  164. if (duplex == DUPLEX_FULL) {
  165. val |= (MCMDR_OPMOD | MCMDR_FDUP);
  166. } else {
  167. val |= MCMDR_OPMOD;
  168. val &= ~MCMDR_FDUP;
  169. }
  170. } else {
  171. /* 10 full/half duplex */
  172. if (duplex == DUPLEX_FULL) {
  173. val |= MCMDR_FDUP;
  174. val &= ~MCMDR_OPMOD;
  175. } else {
  176. val &= ~(MCMDR_FDUP | MCMDR_OPMOD);
  177. }
  178. }
  179. __raw_writel(val, ether->reg + REG_MCMDR);
  180. }
  181. static void update_linkspeed(struct net_device *dev)
  182. {
  183. struct w90p910_ether *ether = netdev_priv(dev);
  184. struct platform_device *pdev;
  185. unsigned int bmsr, bmcr, lpa, speed, duplex;
  186. pdev = ether->pdev;
  187. if (!mii_link_ok(&ether->mii)) {
  188. ether->linkflag = 0x0;
  189. netif_carrier_off(dev);
  190. dev_warn(&pdev->dev, "%s: Link down.\n", dev->name);
  191. return;
  192. }
  193. if (ether->linkflag == 1)
  194. return;
  195. bmsr = w90p910_mdio_read(dev, ether->mii.phy_id, MII_BMSR);
  196. bmcr = w90p910_mdio_read(dev, ether->mii.phy_id, MII_BMCR);
  197. if (bmcr & BMCR_ANENABLE) {
  198. if (!(bmsr & BMSR_ANEGCOMPLETE))
  199. return;
  200. lpa = w90p910_mdio_read(dev, ether->mii.phy_id, MII_LPA);
  201. if ((lpa & LPA_100FULL) || (lpa & LPA_100HALF))
  202. speed = SPEED_100;
  203. else
  204. speed = SPEED_10;
  205. if ((lpa & LPA_100FULL) || (lpa & LPA_10FULL))
  206. duplex = DUPLEX_FULL;
  207. else
  208. duplex = DUPLEX_HALF;
  209. } else {
  210. speed = (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
  211. duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
  212. }
  213. update_linkspeed_register(dev, speed, duplex);
  214. dev_info(&pdev->dev, "%s: Link now %i-%s\n", dev->name, speed,
  215. (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");
  216. ether->linkflag = 0x01;
  217. netif_carrier_on(dev);
  218. }
  219. static void w90p910_check_link(unsigned long dev_id)
  220. {
  221. struct net_device *dev = (struct net_device *) dev_id;
  222. struct w90p910_ether *ether = netdev_priv(dev);
  223. update_linkspeed(dev);
  224. mod_timer(&ether->check_timer, jiffies + msecs_to_jiffies(1000));
  225. }
  226. static void w90p910_write_cam(struct net_device *dev,
  227. unsigned int x, unsigned char *pval)
  228. {
  229. struct w90p910_ether *ether = netdev_priv(dev);
  230. unsigned int msw, lsw;
  231. msw = (pval[0] << 24) | (pval[1] << 16) | (pval[2] << 8) | pval[3];
  232. lsw = (pval[4] << 24) | (pval[5] << 16);
  233. __raw_writel(lsw, ether->reg + REG_CAML_BASE + x * CAM_ENTRY_SIZE);
  234. __raw_writel(msw, ether->reg + REG_CAMM_BASE + x * CAM_ENTRY_SIZE);
  235. }
  236. static int w90p910_init_desc(struct net_device *dev)
  237. {
  238. struct w90p910_ether *ether;
  239. struct w90p910_txbd *tdesc;
  240. struct w90p910_rxbd *rdesc;
  241. struct platform_device *pdev;
  242. unsigned int i;
  243. ether = netdev_priv(dev);
  244. pdev = ether->pdev;
  245. ether->tdesc = dma_alloc_coherent(&pdev->dev, sizeof(struct tran_pdesc),
  246. &ether->tdesc_phys, GFP_KERNEL);
  247. if (!ether->tdesc)
  248. return -ENOMEM;
  249. ether->rdesc = dma_alloc_coherent(&pdev->dev, sizeof(struct recv_pdesc),
  250. &ether->rdesc_phys, GFP_KERNEL);
  251. if (!ether->rdesc) {
  252. dma_free_coherent(&pdev->dev, sizeof(struct tran_pdesc),
  253. ether->tdesc, ether->tdesc_phys);
  254. return -ENOMEM;
  255. }
  256. for (i = 0; i < TX_DESC_SIZE; i++) {
  257. unsigned int offset;
  258. tdesc = &(ether->tdesc->desclist[i]);
  259. if (i == TX_DESC_SIZE - 1)
  260. offset = offsetof(struct tran_pdesc, desclist[0]);
  261. else
  262. offset = offsetof(struct tran_pdesc, desclist[i + 1]);
  263. tdesc->next = ether->tdesc_phys + offset;
  264. tdesc->buffer = ether->tdesc_phys +
  265. offsetof(struct tran_pdesc, tran_buf[i]);
  266. tdesc->sl = 0;
  267. tdesc->mode = 0;
  268. }
  269. ether->start_tx_ptr = ether->tdesc_phys;
  270. for (i = 0; i < RX_DESC_SIZE; i++) {
  271. unsigned int offset;
  272. rdesc = &(ether->rdesc->desclist[i]);
  273. if (i == RX_DESC_SIZE - 1)
  274. offset = offsetof(struct recv_pdesc, desclist[0]);
  275. else
  276. offset = offsetof(struct recv_pdesc, desclist[i + 1]);
  277. rdesc->next = ether->rdesc_phys + offset;
  278. rdesc->sl = RX_OWEN_DMA;
  279. rdesc->buffer = ether->rdesc_phys +
  280. offsetof(struct recv_pdesc, recv_buf[i]);
  281. }
  282. ether->start_rx_ptr = ether->rdesc_phys;
  283. return 0;
  284. }
  285. static void w90p910_set_fifo_threshold(struct net_device *dev)
  286. {
  287. struct w90p910_ether *ether = netdev_priv(dev);
  288. unsigned int val;
  289. val = TXTHD | BLENGTH;
  290. __raw_writel(val, ether->reg + REG_FFTCR);
  291. }
  292. static void w90p910_return_default_idle(struct net_device *dev)
  293. {
  294. struct w90p910_ether *ether = netdev_priv(dev);
  295. unsigned int val;
  296. val = __raw_readl(ether->reg + REG_MCMDR);
  297. val |= SWR;
  298. __raw_writel(val, ether->reg + REG_MCMDR);
  299. }
  300. static void w90p910_trigger_rx(struct net_device *dev)
  301. {
  302. struct w90p910_ether *ether = netdev_priv(dev);
  303. __raw_writel(ENSTART, ether->reg + REG_RSDR);
  304. }
  305. static void w90p910_trigger_tx(struct net_device *dev)
  306. {
  307. struct w90p910_ether *ether = netdev_priv(dev);
  308. __raw_writel(ENSTART, ether->reg + REG_TSDR);
  309. }
  310. static void w90p910_enable_mac_interrupt(struct net_device *dev)
  311. {
  312. struct w90p910_ether *ether = netdev_priv(dev);
  313. unsigned int val;
  314. val = ENTXINTR | ENRXINTR | ENRXGD | ENTXCP;
  315. val |= ENTXBERR | ENRXBERR | ENTXABT;
  316. __raw_writel(val, ether->reg + REG_MIEN);
  317. }
  318. static void w90p910_get_and_clear_int(struct net_device *dev,
  319. unsigned int *val)
  320. {
  321. struct w90p910_ether *ether = netdev_priv(dev);
  322. *val = __raw_readl(ether->reg + REG_MISTA);
  323. __raw_writel(*val, ether->reg + REG_MISTA);
  324. }
  325. static void w90p910_set_global_maccmd(struct net_device *dev)
  326. {
  327. struct w90p910_ether *ether = netdev_priv(dev);
  328. unsigned int val;
  329. val = __raw_readl(ether->reg + REG_MCMDR);
  330. val |= MCMDR_SPCRC | MCMDR_ENMDC | MCMDR_ACP | ENMDC;
  331. __raw_writel(val, ether->reg + REG_MCMDR);
  332. }
  333. static void w90p910_enable_cam(struct net_device *dev)
  334. {
  335. struct w90p910_ether *ether = netdev_priv(dev);
  336. unsigned int val;
  337. w90p910_write_cam(dev, CAM0, dev->dev_addr);
  338. val = __raw_readl(ether->reg + REG_CAMEN);
  339. val |= CAM0EN;
  340. __raw_writel(val, ether->reg + REG_CAMEN);
  341. }
  342. static void w90p910_enable_cam_command(struct net_device *dev)
  343. {
  344. struct w90p910_ether *ether = netdev_priv(dev);
  345. unsigned int val;
  346. val = CAMCMR_ECMP | CAMCMR_ABP | CAMCMR_AMP;
  347. __raw_writel(val, ether->reg + REG_CAMCMR);
  348. }
  349. static void w90p910_enable_tx(struct net_device *dev, unsigned int enable)
  350. {
  351. struct w90p910_ether *ether = netdev_priv(dev);
  352. unsigned int val;
  353. val = __raw_readl(ether->reg + REG_MCMDR);
  354. if (enable)
  355. val |= MCMDR_TXON;
  356. else
  357. val &= ~MCMDR_TXON;
  358. __raw_writel(val, ether->reg + REG_MCMDR);
  359. }
  360. static void w90p910_enable_rx(struct net_device *dev, unsigned int enable)
  361. {
  362. struct w90p910_ether *ether = netdev_priv(dev);
  363. unsigned int val;
  364. val = __raw_readl(ether->reg + REG_MCMDR);
  365. if (enable)
  366. val |= MCMDR_RXON;
  367. else
  368. val &= ~MCMDR_RXON;
  369. __raw_writel(val, ether->reg + REG_MCMDR);
  370. }
  371. static void w90p910_set_curdest(struct net_device *dev)
  372. {
  373. struct w90p910_ether *ether = netdev_priv(dev);
  374. __raw_writel(ether->start_rx_ptr, ether->reg + REG_RXDLSA);
  375. __raw_writel(ether->start_tx_ptr, ether->reg + REG_TXDLSA);
  376. }
  377. static void w90p910_reset_mac(struct net_device *dev)
  378. {
  379. struct w90p910_ether *ether = netdev_priv(dev);
  380. w90p910_enable_tx(dev, 0);
  381. w90p910_enable_rx(dev, 0);
  382. w90p910_set_fifo_threshold(dev);
  383. w90p910_return_default_idle(dev);
  384. if (!netif_queue_stopped(dev))
  385. netif_stop_queue(dev);
  386. w90p910_init_desc(dev);
  387. netif_trans_update(dev); /* prevent tx timeout */
  388. ether->cur_tx = 0x0;
  389. ether->finish_tx = 0x0;
  390. ether->cur_rx = 0x0;
  391. w90p910_set_curdest(dev);
  392. w90p910_enable_cam(dev);
  393. w90p910_enable_cam_command(dev);
  394. w90p910_enable_mac_interrupt(dev);
  395. w90p910_enable_tx(dev, 1);
  396. w90p910_enable_rx(dev, 1);
  397. w90p910_trigger_tx(dev);
  398. w90p910_trigger_rx(dev);
  399. netif_trans_update(dev); /* prevent tx timeout */
  400. if (netif_queue_stopped(dev))
  401. netif_wake_queue(dev);
  402. }
  403. static void w90p910_mdio_write(struct net_device *dev,
  404. int phy_id, int reg, int data)
  405. {
  406. struct w90p910_ether *ether = netdev_priv(dev);
  407. struct platform_device *pdev;
  408. unsigned int val, i;
  409. pdev = ether->pdev;
  410. __raw_writel(data, ether->reg + REG_MIID);
  411. val = (phy_id << 0x08) | reg;
  412. val |= PHYBUSY | PHYWR | MDCCR_VAL;
  413. __raw_writel(val, ether->reg + REG_MIIDA);
  414. for (i = 0; i < DELAY; i++) {
  415. if ((__raw_readl(ether->reg + REG_MIIDA) & PHYBUSY) == 0)
  416. break;
  417. }
  418. if (i == DELAY)
  419. dev_warn(&pdev->dev, "mdio write timed out\n");
  420. }
  421. static int w90p910_mdio_read(struct net_device *dev, int phy_id, int reg)
  422. {
  423. struct w90p910_ether *ether = netdev_priv(dev);
  424. struct platform_device *pdev;
  425. unsigned int val, i, data;
  426. pdev = ether->pdev;
  427. val = (phy_id << 0x08) | reg;
  428. val |= PHYBUSY | MDCCR_VAL;
  429. __raw_writel(val, ether->reg + REG_MIIDA);
  430. for (i = 0; i < DELAY; i++) {
  431. if ((__raw_readl(ether->reg + REG_MIIDA) & PHYBUSY) == 0)
  432. break;
  433. }
  434. if (i == DELAY) {
  435. dev_warn(&pdev->dev, "mdio read timed out\n");
  436. data = 0xffff;
  437. } else {
  438. data = __raw_readl(ether->reg + REG_MIID);
  439. }
  440. return data;
  441. }
  442. static int w90p910_set_mac_address(struct net_device *dev, void *addr)
  443. {
  444. struct sockaddr *address = addr;
  445. if (!is_valid_ether_addr(address->sa_data))
  446. return -EADDRNOTAVAIL;
  447. memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
  448. w90p910_write_cam(dev, CAM0, dev->dev_addr);
  449. return 0;
  450. }
  451. static int w90p910_ether_close(struct net_device *dev)
  452. {
  453. struct w90p910_ether *ether = netdev_priv(dev);
  454. struct platform_device *pdev;
  455. pdev = ether->pdev;
  456. dma_free_coherent(&pdev->dev, sizeof(struct recv_pdesc),
  457. ether->rdesc, ether->rdesc_phys);
  458. dma_free_coherent(&pdev->dev, sizeof(struct tran_pdesc),
  459. ether->tdesc, ether->tdesc_phys);
  460. netif_stop_queue(dev);
  461. del_timer_sync(&ether->check_timer);
  462. clk_disable(ether->rmiiclk);
  463. clk_disable(ether->clk);
  464. free_irq(ether->txirq, dev);
  465. free_irq(ether->rxirq, dev);
  466. return 0;
  467. }
  468. static struct net_device_stats *w90p910_ether_stats(struct net_device *dev)
  469. {
  470. struct w90p910_ether *ether;
  471. ether = netdev_priv(dev);
  472. return &ether->stats;
  473. }
  474. static int w90p910_send_frame(struct net_device *dev,
  475. unsigned char *data, int length)
  476. {
  477. struct w90p910_ether *ether;
  478. struct w90p910_txbd *txbd;
  479. struct platform_device *pdev;
  480. unsigned char *buffer;
  481. ether = netdev_priv(dev);
  482. pdev = ether->pdev;
  483. txbd = &ether->tdesc->desclist[ether->cur_tx];
  484. buffer = ether->tdesc->tran_buf[ether->cur_tx];
  485. if (length > 1514) {
  486. dev_err(&pdev->dev, "send data %d bytes, check it\n", length);
  487. length = 1514;
  488. }
  489. txbd->sl = length & 0xFFFF;
  490. memcpy(buffer, data, length);
  491. txbd->mode = TX_OWEN_DMA | PADDINGMODE | CRCMODE | MACTXINTEN;
  492. w90p910_enable_tx(dev, 1);
  493. w90p910_trigger_tx(dev);
  494. if (++ether->cur_tx >= TX_DESC_SIZE)
  495. ether->cur_tx = 0;
  496. txbd = &ether->tdesc->desclist[ether->cur_tx];
  497. if (txbd->mode & TX_OWEN_DMA)
  498. netif_stop_queue(dev);
  499. return 0;
  500. }
  501. static int w90p910_ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  502. {
  503. struct w90p910_ether *ether = netdev_priv(dev);
  504. if (!(w90p910_send_frame(dev, skb->data, skb->len))) {
  505. ether->skb = skb;
  506. dev_kfree_skb_irq(skb);
  507. return 0;
  508. }
  509. return -EAGAIN;
  510. }
  511. static irqreturn_t w90p910_tx_interrupt(int irq, void *dev_id)
  512. {
  513. struct w90p910_ether *ether;
  514. struct w90p910_txbd *txbd;
  515. struct platform_device *pdev;
  516. struct net_device *dev;
  517. unsigned int cur_entry, entry, status;
  518. dev = dev_id;
  519. ether = netdev_priv(dev);
  520. pdev = ether->pdev;
  521. w90p910_get_and_clear_int(dev, &status);
  522. cur_entry = __raw_readl(ether->reg + REG_CTXDSA);
  523. entry = ether->tdesc_phys +
  524. offsetof(struct tran_pdesc, desclist[ether->finish_tx]);
  525. while (entry != cur_entry) {
  526. txbd = &ether->tdesc->desclist[ether->finish_tx];
  527. if (++ether->finish_tx >= TX_DESC_SIZE)
  528. ether->finish_tx = 0;
  529. if (txbd->sl & TXDS_TXCP) {
  530. ether->stats.tx_packets++;
  531. ether->stats.tx_bytes += txbd->sl & 0xFFFF;
  532. } else {
  533. ether->stats.tx_errors++;
  534. }
  535. txbd->sl = 0x0;
  536. txbd->mode = 0x0;
  537. if (netif_queue_stopped(dev))
  538. netif_wake_queue(dev);
  539. entry = ether->tdesc_phys +
  540. offsetof(struct tran_pdesc, desclist[ether->finish_tx]);
  541. }
  542. if (status & MISTA_EXDEF) {
  543. dev_err(&pdev->dev, "emc defer exceed interrupt\n");
  544. } else if (status & MISTA_TXBERR) {
  545. dev_err(&pdev->dev, "emc bus error interrupt\n");
  546. w90p910_reset_mac(dev);
  547. } else if (status & MISTA_TDU) {
  548. if (netif_queue_stopped(dev))
  549. netif_wake_queue(dev);
  550. }
  551. return IRQ_HANDLED;
  552. }
  553. static void netdev_rx(struct net_device *dev)
  554. {
  555. struct w90p910_ether *ether;
  556. struct w90p910_rxbd *rxbd;
  557. struct platform_device *pdev;
  558. struct sk_buff *skb;
  559. unsigned char *data;
  560. unsigned int length, status, val, entry;
  561. ether = netdev_priv(dev);
  562. pdev = ether->pdev;
  563. rxbd = &ether->rdesc->desclist[ether->cur_rx];
  564. do {
  565. val = __raw_readl(ether->reg + REG_CRXDSA);
  566. entry = ether->rdesc_phys +
  567. offsetof(struct recv_pdesc, desclist[ether->cur_rx]);
  568. if (val == entry)
  569. break;
  570. status = rxbd->sl;
  571. length = status & 0xFFFF;
  572. if (status & RXDS_RXGD) {
  573. data = ether->rdesc->recv_buf[ether->cur_rx];
  574. skb = netdev_alloc_skb(dev, length + 2);
  575. if (!skb) {
  576. ether->stats.rx_dropped++;
  577. return;
  578. }
  579. skb_reserve(skb, 2);
  580. skb_put(skb, length);
  581. skb_copy_to_linear_data(skb, data, length);
  582. skb->protocol = eth_type_trans(skb, dev);
  583. ether->stats.rx_packets++;
  584. ether->stats.rx_bytes += length;
  585. netif_rx(skb);
  586. } else {
  587. ether->stats.rx_errors++;
  588. if (status & RXDS_RP) {
  589. dev_err(&pdev->dev, "rx runt err\n");
  590. ether->stats.rx_length_errors++;
  591. } else if (status & RXDS_CRCE) {
  592. dev_err(&pdev->dev, "rx crc err\n");
  593. ether->stats.rx_crc_errors++;
  594. } else if (status & RXDS_ALIE) {
  595. dev_err(&pdev->dev, "rx alignment err\n");
  596. ether->stats.rx_frame_errors++;
  597. } else if (status & RXDS_PTLE) {
  598. dev_err(&pdev->dev, "rx longer err\n");
  599. ether->stats.rx_over_errors++;
  600. }
  601. }
  602. rxbd->sl = RX_OWEN_DMA;
  603. rxbd->reserved = 0x0;
  604. if (++ether->cur_rx >= RX_DESC_SIZE)
  605. ether->cur_rx = 0;
  606. rxbd = &ether->rdesc->desclist[ether->cur_rx];
  607. } while (1);
  608. }
  609. static irqreturn_t w90p910_rx_interrupt(int irq, void *dev_id)
  610. {
  611. struct net_device *dev;
  612. struct w90p910_ether *ether;
  613. struct platform_device *pdev;
  614. unsigned int status;
  615. dev = dev_id;
  616. ether = netdev_priv(dev);
  617. pdev = ether->pdev;
  618. w90p910_get_and_clear_int(dev, &status);
  619. if (status & MISTA_RDU) {
  620. netdev_rx(dev);
  621. w90p910_trigger_rx(dev);
  622. return IRQ_HANDLED;
  623. } else if (status & MISTA_RXBERR) {
  624. dev_err(&pdev->dev, "emc rx bus error\n");
  625. w90p910_reset_mac(dev);
  626. }
  627. netdev_rx(dev);
  628. return IRQ_HANDLED;
  629. }
  630. static int w90p910_ether_open(struct net_device *dev)
  631. {
  632. struct w90p910_ether *ether;
  633. struct platform_device *pdev;
  634. ether = netdev_priv(dev);
  635. pdev = ether->pdev;
  636. w90p910_reset_mac(dev);
  637. w90p910_set_fifo_threshold(dev);
  638. w90p910_set_curdest(dev);
  639. w90p910_enable_cam(dev);
  640. w90p910_enable_cam_command(dev);
  641. w90p910_enable_mac_interrupt(dev);
  642. w90p910_set_global_maccmd(dev);
  643. w90p910_enable_rx(dev, 1);
  644. clk_enable(ether->rmiiclk);
  645. clk_enable(ether->clk);
  646. ether->rx_packets = 0x0;
  647. ether->rx_bytes = 0x0;
  648. if (request_irq(ether->txirq, w90p910_tx_interrupt,
  649. 0x0, pdev->name, dev)) {
  650. dev_err(&pdev->dev, "register irq tx failed\n");
  651. return -EAGAIN;
  652. }
  653. if (request_irq(ether->rxirq, w90p910_rx_interrupt,
  654. 0x0, pdev->name, dev)) {
  655. dev_err(&pdev->dev, "register irq rx failed\n");
  656. free_irq(ether->txirq, dev);
  657. return -EAGAIN;
  658. }
  659. mod_timer(&ether->check_timer, jiffies + msecs_to_jiffies(1000));
  660. netif_start_queue(dev);
  661. w90p910_trigger_rx(dev);
  662. dev_info(&pdev->dev, "%s is OPENED\n", dev->name);
  663. return 0;
  664. }
  665. static void w90p910_ether_set_multicast_list(struct net_device *dev)
  666. {
  667. struct w90p910_ether *ether;
  668. unsigned int rx_mode;
  669. ether = netdev_priv(dev);
  670. if (dev->flags & IFF_PROMISC)
  671. rx_mode = CAMCMR_AUP | CAMCMR_AMP | CAMCMR_ABP | CAMCMR_ECMP;
  672. else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
  673. rx_mode = CAMCMR_AMP | CAMCMR_ABP | CAMCMR_ECMP;
  674. else
  675. rx_mode = CAMCMR_ECMP | CAMCMR_ABP;
  676. __raw_writel(rx_mode, ether->reg + REG_CAMCMR);
  677. }
  678. static int w90p910_ether_ioctl(struct net_device *dev,
  679. struct ifreq *ifr, int cmd)
  680. {
  681. struct w90p910_ether *ether = netdev_priv(dev);
  682. struct mii_ioctl_data *data = if_mii(ifr);
  683. return generic_mii_ioctl(&ether->mii, data, cmd, NULL);
  684. }
  685. static void w90p910_get_drvinfo(struct net_device *dev,
  686. struct ethtool_drvinfo *info)
  687. {
  688. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  689. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  690. }
  691. static int w90p910_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  692. {
  693. struct w90p910_ether *ether = netdev_priv(dev);
  694. return mii_ethtool_gset(&ether->mii, cmd);
  695. }
  696. static int w90p910_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  697. {
  698. struct w90p910_ether *ether = netdev_priv(dev);
  699. return mii_ethtool_sset(&ether->mii, cmd);
  700. }
  701. static int w90p910_nway_reset(struct net_device *dev)
  702. {
  703. struct w90p910_ether *ether = netdev_priv(dev);
  704. return mii_nway_restart(&ether->mii);
  705. }
  706. static u32 w90p910_get_link(struct net_device *dev)
  707. {
  708. struct w90p910_ether *ether = netdev_priv(dev);
  709. return mii_link_ok(&ether->mii);
  710. }
  711. static const struct ethtool_ops w90p910_ether_ethtool_ops = {
  712. .get_settings = w90p910_get_settings,
  713. .set_settings = w90p910_set_settings,
  714. .get_drvinfo = w90p910_get_drvinfo,
  715. .nway_reset = w90p910_nway_reset,
  716. .get_link = w90p910_get_link,
  717. };
  718. static const struct net_device_ops w90p910_ether_netdev_ops = {
  719. .ndo_open = w90p910_ether_open,
  720. .ndo_stop = w90p910_ether_close,
  721. .ndo_start_xmit = w90p910_ether_start_xmit,
  722. .ndo_get_stats = w90p910_ether_stats,
  723. .ndo_set_rx_mode = w90p910_ether_set_multicast_list,
  724. .ndo_set_mac_address = w90p910_set_mac_address,
  725. .ndo_do_ioctl = w90p910_ether_ioctl,
  726. .ndo_validate_addr = eth_validate_addr,
  727. .ndo_change_mtu = eth_change_mtu,
  728. };
  729. static void __init get_mac_address(struct net_device *dev)
  730. {
  731. struct w90p910_ether *ether = netdev_priv(dev);
  732. struct platform_device *pdev;
  733. char addr[ETH_ALEN];
  734. pdev = ether->pdev;
  735. addr[0] = 0x00;
  736. addr[1] = 0x02;
  737. addr[2] = 0xac;
  738. addr[3] = 0x55;
  739. addr[4] = 0x88;
  740. addr[5] = 0xa8;
  741. if (is_valid_ether_addr(addr))
  742. memcpy(dev->dev_addr, &addr, ETH_ALEN);
  743. else
  744. dev_err(&pdev->dev, "invalid mac address\n");
  745. }
  746. static int w90p910_ether_setup(struct net_device *dev)
  747. {
  748. struct w90p910_ether *ether = netdev_priv(dev);
  749. dev->netdev_ops = &w90p910_ether_netdev_ops;
  750. dev->ethtool_ops = &w90p910_ether_ethtool_ops;
  751. dev->tx_queue_len = 16;
  752. dev->dma = 0x0;
  753. dev->watchdog_timeo = TX_TIMEOUT;
  754. get_mac_address(dev);
  755. ether->cur_tx = 0x0;
  756. ether->cur_rx = 0x0;
  757. ether->finish_tx = 0x0;
  758. ether->linkflag = 0x0;
  759. ether->mii.phy_id = 0x01;
  760. ether->mii.phy_id_mask = 0x1f;
  761. ether->mii.reg_num_mask = 0x1f;
  762. ether->mii.dev = dev;
  763. ether->mii.mdio_read = w90p910_mdio_read;
  764. ether->mii.mdio_write = w90p910_mdio_write;
  765. setup_timer(&ether->check_timer, w90p910_check_link,
  766. (unsigned long)dev);
  767. return 0;
  768. }
  769. static int w90p910_ether_probe(struct platform_device *pdev)
  770. {
  771. struct w90p910_ether *ether;
  772. struct net_device *dev;
  773. int error;
  774. dev = alloc_etherdev(sizeof(struct w90p910_ether));
  775. if (!dev)
  776. return -ENOMEM;
  777. ether = netdev_priv(dev);
  778. ether->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  779. if (ether->res == NULL) {
  780. dev_err(&pdev->dev, "failed to get I/O memory\n");
  781. error = -ENXIO;
  782. goto failed_free;
  783. }
  784. if (!request_mem_region(ether->res->start,
  785. resource_size(ether->res), pdev->name)) {
  786. dev_err(&pdev->dev, "failed to request I/O memory\n");
  787. error = -EBUSY;
  788. goto failed_free;
  789. }
  790. ether->reg = ioremap(ether->res->start, resource_size(ether->res));
  791. if (ether->reg == NULL) {
  792. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  793. error = -ENXIO;
  794. goto failed_free_mem;
  795. }
  796. ether->txirq = platform_get_irq(pdev, 0);
  797. if (ether->txirq < 0) {
  798. dev_err(&pdev->dev, "failed to get ether tx irq\n");
  799. error = -ENXIO;
  800. goto failed_free_io;
  801. }
  802. ether->rxirq = platform_get_irq(pdev, 1);
  803. if (ether->rxirq < 0) {
  804. dev_err(&pdev->dev, "failed to get ether rx irq\n");
  805. error = -ENXIO;
  806. goto failed_free_io;
  807. }
  808. platform_set_drvdata(pdev, dev);
  809. ether->clk = clk_get(&pdev->dev, NULL);
  810. if (IS_ERR(ether->clk)) {
  811. dev_err(&pdev->dev, "failed to get ether clock\n");
  812. error = PTR_ERR(ether->clk);
  813. goto failed_free_io;
  814. }
  815. ether->rmiiclk = clk_get(&pdev->dev, "RMII");
  816. if (IS_ERR(ether->rmiiclk)) {
  817. dev_err(&pdev->dev, "failed to get ether clock\n");
  818. error = PTR_ERR(ether->rmiiclk);
  819. goto failed_put_clk;
  820. }
  821. ether->pdev = pdev;
  822. w90p910_ether_setup(dev);
  823. error = register_netdev(dev);
  824. if (error != 0) {
  825. dev_err(&pdev->dev, "Register EMC w90p910 FAILED\n");
  826. error = -ENODEV;
  827. goto failed_put_rmiiclk;
  828. }
  829. return 0;
  830. failed_put_rmiiclk:
  831. clk_put(ether->rmiiclk);
  832. failed_put_clk:
  833. clk_put(ether->clk);
  834. failed_free_io:
  835. iounmap(ether->reg);
  836. failed_free_mem:
  837. release_mem_region(ether->res->start, resource_size(ether->res));
  838. failed_free:
  839. free_netdev(dev);
  840. return error;
  841. }
  842. static int w90p910_ether_remove(struct platform_device *pdev)
  843. {
  844. struct net_device *dev = platform_get_drvdata(pdev);
  845. struct w90p910_ether *ether = netdev_priv(dev);
  846. unregister_netdev(dev);
  847. clk_put(ether->rmiiclk);
  848. clk_put(ether->clk);
  849. iounmap(ether->reg);
  850. release_mem_region(ether->res->start, resource_size(ether->res));
  851. del_timer_sync(&ether->check_timer);
  852. free_netdev(dev);
  853. return 0;
  854. }
  855. static struct platform_driver w90p910_ether_driver = {
  856. .probe = w90p910_ether_probe,
  857. .remove = w90p910_ether_remove,
  858. .driver = {
  859. .name = "nuc900-emc",
  860. },
  861. };
  862. module_platform_driver(w90p910_ether_driver);
  863. MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
  864. MODULE_DESCRIPTION("w90p910 MAC driver!");
  865. MODULE_LICENSE("GPL");
  866. MODULE_ALIAS("platform:nuc900-emc");