w90p910_ether.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 = (struct tran_pdesc *)
  246. dma_alloc_coherent(&pdev->dev, sizeof(struct tran_pdesc),
  247. &ether->tdesc_phys, GFP_KERNEL);
  248. if (!ether->tdesc) {
  249. dev_err(&pdev->dev, "Failed to allocate memory for tx desc\n");
  250. return -ENOMEM;
  251. }
  252. ether->rdesc = (struct recv_pdesc *)
  253. dma_alloc_coherent(&pdev->dev, sizeof(struct recv_pdesc),
  254. &ether->rdesc_phys, GFP_KERNEL);
  255. if (!ether->rdesc) {
  256. dev_err(&pdev->dev, "Failed to allocate memory for rx desc\n");
  257. dma_free_coherent(&pdev->dev, sizeof(struct tran_pdesc),
  258. ether->tdesc, ether->tdesc_phys);
  259. return -ENOMEM;
  260. }
  261. for (i = 0; i < TX_DESC_SIZE; i++) {
  262. unsigned int offset;
  263. tdesc = &(ether->tdesc->desclist[i]);
  264. if (i == TX_DESC_SIZE - 1)
  265. offset = offsetof(struct tran_pdesc, desclist[0]);
  266. else
  267. offset = offsetof(struct tran_pdesc, desclist[i + 1]);
  268. tdesc->next = ether->tdesc_phys + offset;
  269. tdesc->buffer = ether->tdesc_phys +
  270. offsetof(struct tran_pdesc, tran_buf[i]);
  271. tdesc->sl = 0;
  272. tdesc->mode = 0;
  273. }
  274. ether->start_tx_ptr = ether->tdesc_phys;
  275. for (i = 0; i < RX_DESC_SIZE; i++) {
  276. unsigned int offset;
  277. rdesc = &(ether->rdesc->desclist[i]);
  278. if (i == RX_DESC_SIZE - 1)
  279. offset = offsetof(struct recv_pdesc, desclist[0]);
  280. else
  281. offset = offsetof(struct recv_pdesc, desclist[i + 1]);
  282. rdesc->next = ether->rdesc_phys + offset;
  283. rdesc->sl = RX_OWEN_DMA;
  284. rdesc->buffer = ether->rdesc_phys +
  285. offsetof(struct recv_pdesc, recv_buf[i]);
  286. }
  287. ether->start_rx_ptr = ether->rdesc_phys;
  288. return 0;
  289. }
  290. static void w90p910_set_fifo_threshold(struct net_device *dev)
  291. {
  292. struct w90p910_ether *ether = netdev_priv(dev);
  293. unsigned int val;
  294. val = TXTHD | BLENGTH;
  295. __raw_writel(val, ether->reg + REG_FFTCR);
  296. }
  297. static void w90p910_return_default_idle(struct net_device *dev)
  298. {
  299. struct w90p910_ether *ether = netdev_priv(dev);
  300. unsigned int val;
  301. val = __raw_readl(ether->reg + REG_MCMDR);
  302. val |= SWR;
  303. __raw_writel(val, ether->reg + REG_MCMDR);
  304. }
  305. static void w90p910_trigger_rx(struct net_device *dev)
  306. {
  307. struct w90p910_ether *ether = netdev_priv(dev);
  308. __raw_writel(ENSTART, ether->reg + REG_RSDR);
  309. }
  310. static void w90p910_trigger_tx(struct net_device *dev)
  311. {
  312. struct w90p910_ether *ether = netdev_priv(dev);
  313. __raw_writel(ENSTART, ether->reg + REG_TSDR);
  314. }
  315. static void w90p910_enable_mac_interrupt(struct net_device *dev)
  316. {
  317. struct w90p910_ether *ether = netdev_priv(dev);
  318. unsigned int val;
  319. val = ENTXINTR | ENRXINTR | ENRXGD | ENTXCP;
  320. val |= ENTXBERR | ENRXBERR | ENTXABT;
  321. __raw_writel(val, ether->reg + REG_MIEN);
  322. }
  323. static void w90p910_get_and_clear_int(struct net_device *dev,
  324. unsigned int *val)
  325. {
  326. struct w90p910_ether *ether = netdev_priv(dev);
  327. *val = __raw_readl(ether->reg + REG_MISTA);
  328. __raw_writel(*val, ether->reg + REG_MISTA);
  329. }
  330. static void w90p910_set_global_maccmd(struct net_device *dev)
  331. {
  332. struct w90p910_ether *ether = netdev_priv(dev);
  333. unsigned int val;
  334. val = __raw_readl(ether->reg + REG_MCMDR);
  335. val |= MCMDR_SPCRC | MCMDR_ENMDC | MCMDR_ACP | ENMDC;
  336. __raw_writel(val, ether->reg + REG_MCMDR);
  337. }
  338. static void w90p910_enable_cam(struct net_device *dev)
  339. {
  340. struct w90p910_ether *ether = netdev_priv(dev);
  341. unsigned int val;
  342. w90p910_write_cam(dev, CAM0, dev->dev_addr);
  343. val = __raw_readl(ether->reg + REG_CAMEN);
  344. val |= CAM0EN;
  345. __raw_writel(val, ether->reg + REG_CAMEN);
  346. }
  347. static void w90p910_enable_cam_command(struct net_device *dev)
  348. {
  349. struct w90p910_ether *ether = netdev_priv(dev);
  350. unsigned int val;
  351. val = CAMCMR_ECMP | CAMCMR_ABP | CAMCMR_AMP;
  352. __raw_writel(val, ether->reg + REG_CAMCMR);
  353. }
  354. static void w90p910_enable_tx(struct net_device *dev, unsigned int enable)
  355. {
  356. struct w90p910_ether *ether = netdev_priv(dev);
  357. unsigned int val;
  358. val = __raw_readl(ether->reg + REG_MCMDR);
  359. if (enable)
  360. val |= MCMDR_TXON;
  361. else
  362. val &= ~MCMDR_TXON;
  363. __raw_writel(val, ether->reg + REG_MCMDR);
  364. }
  365. static void w90p910_enable_rx(struct net_device *dev, unsigned int enable)
  366. {
  367. struct w90p910_ether *ether = netdev_priv(dev);
  368. unsigned int val;
  369. val = __raw_readl(ether->reg + REG_MCMDR);
  370. if (enable)
  371. val |= MCMDR_RXON;
  372. else
  373. val &= ~MCMDR_RXON;
  374. __raw_writel(val, ether->reg + REG_MCMDR);
  375. }
  376. static void w90p910_set_curdest(struct net_device *dev)
  377. {
  378. struct w90p910_ether *ether = netdev_priv(dev);
  379. __raw_writel(ether->start_rx_ptr, ether->reg + REG_RXDLSA);
  380. __raw_writel(ether->start_tx_ptr, ether->reg + REG_TXDLSA);
  381. }
  382. static void w90p910_reset_mac(struct net_device *dev)
  383. {
  384. struct w90p910_ether *ether = netdev_priv(dev);
  385. w90p910_enable_tx(dev, 0);
  386. w90p910_enable_rx(dev, 0);
  387. w90p910_set_fifo_threshold(dev);
  388. w90p910_return_default_idle(dev);
  389. if (!netif_queue_stopped(dev))
  390. netif_stop_queue(dev);
  391. w90p910_init_desc(dev);
  392. dev->trans_start = jiffies; /* prevent tx timeout */
  393. ether->cur_tx = 0x0;
  394. ether->finish_tx = 0x0;
  395. ether->cur_rx = 0x0;
  396. w90p910_set_curdest(dev);
  397. w90p910_enable_cam(dev);
  398. w90p910_enable_cam_command(dev);
  399. w90p910_enable_mac_interrupt(dev);
  400. w90p910_enable_tx(dev, 1);
  401. w90p910_enable_rx(dev, 1);
  402. w90p910_trigger_tx(dev);
  403. w90p910_trigger_rx(dev);
  404. dev->trans_start = jiffies; /* prevent tx timeout */
  405. if (netif_queue_stopped(dev))
  406. netif_wake_queue(dev);
  407. }
  408. static void w90p910_mdio_write(struct net_device *dev,
  409. int phy_id, int reg, int data)
  410. {
  411. struct w90p910_ether *ether = netdev_priv(dev);
  412. struct platform_device *pdev;
  413. unsigned int val, i;
  414. pdev = ether->pdev;
  415. __raw_writel(data, ether->reg + REG_MIID);
  416. val = (phy_id << 0x08) | reg;
  417. val |= PHYBUSY | PHYWR | MDCCR_VAL;
  418. __raw_writel(val, ether->reg + REG_MIIDA);
  419. for (i = 0; i < DELAY; i++) {
  420. if ((__raw_readl(ether->reg + REG_MIIDA) & PHYBUSY) == 0)
  421. break;
  422. }
  423. if (i == DELAY)
  424. dev_warn(&pdev->dev, "mdio write timed out\n");
  425. }
  426. static int w90p910_mdio_read(struct net_device *dev, int phy_id, int reg)
  427. {
  428. struct w90p910_ether *ether = netdev_priv(dev);
  429. struct platform_device *pdev;
  430. unsigned int val, i, data;
  431. pdev = ether->pdev;
  432. val = (phy_id << 0x08) | reg;
  433. val |= PHYBUSY | MDCCR_VAL;
  434. __raw_writel(val, ether->reg + REG_MIIDA);
  435. for (i = 0; i < DELAY; i++) {
  436. if ((__raw_readl(ether->reg + REG_MIIDA) & PHYBUSY) == 0)
  437. break;
  438. }
  439. if (i == DELAY) {
  440. dev_warn(&pdev->dev, "mdio read timed out\n");
  441. data = 0xffff;
  442. } else {
  443. data = __raw_readl(ether->reg + REG_MIID);
  444. }
  445. return data;
  446. }
  447. static int w90p910_set_mac_address(struct net_device *dev, void *addr)
  448. {
  449. struct sockaddr *address = addr;
  450. if (!is_valid_ether_addr(address->sa_data))
  451. return -EADDRNOTAVAIL;
  452. memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
  453. w90p910_write_cam(dev, CAM0, dev->dev_addr);
  454. return 0;
  455. }
  456. static int w90p910_ether_close(struct net_device *dev)
  457. {
  458. struct w90p910_ether *ether = netdev_priv(dev);
  459. struct platform_device *pdev;
  460. pdev = ether->pdev;
  461. dma_free_coherent(&pdev->dev, sizeof(struct recv_pdesc),
  462. ether->rdesc, ether->rdesc_phys);
  463. dma_free_coherent(&pdev->dev, sizeof(struct tran_pdesc),
  464. ether->tdesc, ether->tdesc_phys);
  465. netif_stop_queue(dev);
  466. del_timer_sync(&ether->check_timer);
  467. clk_disable(ether->rmiiclk);
  468. clk_disable(ether->clk);
  469. free_irq(ether->txirq, dev);
  470. free_irq(ether->rxirq, dev);
  471. return 0;
  472. }
  473. static struct net_device_stats *w90p910_ether_stats(struct net_device *dev)
  474. {
  475. struct w90p910_ether *ether;
  476. ether = netdev_priv(dev);
  477. return &ether->stats;
  478. }
  479. static int w90p910_send_frame(struct net_device *dev,
  480. unsigned char *data, int length)
  481. {
  482. struct w90p910_ether *ether;
  483. struct w90p910_txbd *txbd;
  484. struct platform_device *pdev;
  485. unsigned char *buffer;
  486. ether = netdev_priv(dev);
  487. pdev = ether->pdev;
  488. txbd = &ether->tdesc->desclist[ether->cur_tx];
  489. buffer = ether->tdesc->tran_buf[ether->cur_tx];
  490. if (length > 1514) {
  491. dev_err(&pdev->dev, "send data %d bytes, check it\n", length);
  492. length = 1514;
  493. }
  494. txbd->sl = length & 0xFFFF;
  495. memcpy(buffer, data, length);
  496. txbd->mode = TX_OWEN_DMA | PADDINGMODE | CRCMODE | MACTXINTEN;
  497. w90p910_enable_tx(dev, 1);
  498. w90p910_trigger_tx(dev);
  499. if (++ether->cur_tx >= TX_DESC_SIZE)
  500. ether->cur_tx = 0;
  501. txbd = &ether->tdesc->desclist[ether->cur_tx];
  502. if (txbd->mode & TX_OWEN_DMA)
  503. netif_stop_queue(dev);
  504. return 0;
  505. }
  506. static int w90p910_ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
  507. {
  508. struct w90p910_ether *ether = netdev_priv(dev);
  509. if (!(w90p910_send_frame(dev, skb->data, skb->len))) {
  510. ether->skb = skb;
  511. dev_kfree_skb_irq(skb);
  512. return 0;
  513. }
  514. return -EAGAIN;
  515. }
  516. static irqreturn_t w90p910_tx_interrupt(int irq, void *dev_id)
  517. {
  518. struct w90p910_ether *ether;
  519. struct w90p910_txbd *txbd;
  520. struct platform_device *pdev;
  521. struct net_device *dev;
  522. unsigned int cur_entry, entry, status;
  523. dev = dev_id;
  524. ether = netdev_priv(dev);
  525. pdev = ether->pdev;
  526. w90p910_get_and_clear_int(dev, &status);
  527. cur_entry = __raw_readl(ether->reg + REG_CTXDSA);
  528. entry = ether->tdesc_phys +
  529. offsetof(struct tran_pdesc, desclist[ether->finish_tx]);
  530. while (entry != cur_entry) {
  531. txbd = &ether->tdesc->desclist[ether->finish_tx];
  532. if (++ether->finish_tx >= TX_DESC_SIZE)
  533. ether->finish_tx = 0;
  534. if (txbd->sl & TXDS_TXCP) {
  535. ether->stats.tx_packets++;
  536. ether->stats.tx_bytes += txbd->sl & 0xFFFF;
  537. } else {
  538. ether->stats.tx_errors++;
  539. }
  540. txbd->sl = 0x0;
  541. txbd->mode = 0x0;
  542. if (netif_queue_stopped(dev))
  543. netif_wake_queue(dev);
  544. entry = ether->tdesc_phys +
  545. offsetof(struct tran_pdesc, desclist[ether->finish_tx]);
  546. }
  547. if (status & MISTA_EXDEF) {
  548. dev_err(&pdev->dev, "emc defer exceed interrupt\n");
  549. } else if (status & MISTA_TXBERR) {
  550. dev_err(&pdev->dev, "emc bus error interrupt\n");
  551. w90p910_reset_mac(dev);
  552. } else if (status & MISTA_TDU) {
  553. if (netif_queue_stopped(dev))
  554. netif_wake_queue(dev);
  555. }
  556. return IRQ_HANDLED;
  557. }
  558. static void netdev_rx(struct net_device *dev)
  559. {
  560. struct w90p910_ether *ether;
  561. struct w90p910_rxbd *rxbd;
  562. struct platform_device *pdev;
  563. struct sk_buff *skb;
  564. unsigned char *data;
  565. unsigned int length, status, val, entry;
  566. ether = netdev_priv(dev);
  567. pdev = ether->pdev;
  568. rxbd = &ether->rdesc->desclist[ether->cur_rx];
  569. do {
  570. val = __raw_readl(ether->reg + REG_CRXDSA);
  571. entry = ether->rdesc_phys +
  572. offsetof(struct recv_pdesc, desclist[ether->cur_rx]);
  573. if (val == entry)
  574. break;
  575. status = rxbd->sl;
  576. length = status & 0xFFFF;
  577. if (status & RXDS_RXGD) {
  578. data = ether->rdesc->recv_buf[ether->cur_rx];
  579. skb = netdev_alloc_skb(dev, length + 2);
  580. if (!skb) {
  581. dev_err(&pdev->dev, "get skb buffer error\n");
  582. ether->stats.rx_dropped++;
  583. return;
  584. }
  585. skb_reserve(skb, 2);
  586. skb_put(skb, length);
  587. skb_copy_to_linear_data(skb, data, length);
  588. skb->protocol = eth_type_trans(skb, dev);
  589. ether->stats.rx_packets++;
  590. ether->stats.rx_bytes += length;
  591. netif_rx(skb);
  592. } else {
  593. ether->stats.rx_errors++;
  594. if (status & RXDS_RP) {
  595. dev_err(&pdev->dev, "rx runt err\n");
  596. ether->stats.rx_length_errors++;
  597. } else if (status & RXDS_CRCE) {
  598. dev_err(&pdev->dev, "rx crc err\n");
  599. ether->stats.rx_crc_errors++;
  600. } else if (status & RXDS_ALIE) {
  601. dev_err(&pdev->dev, "rx aligment err\n");
  602. ether->stats.rx_frame_errors++;
  603. } else if (status & RXDS_PTLE) {
  604. dev_err(&pdev->dev, "rx longer err\n");
  605. ether->stats.rx_over_errors++;
  606. }
  607. }
  608. rxbd->sl = RX_OWEN_DMA;
  609. rxbd->reserved = 0x0;
  610. if (++ether->cur_rx >= RX_DESC_SIZE)
  611. ether->cur_rx = 0;
  612. rxbd = &ether->rdesc->desclist[ether->cur_rx];
  613. } while (1);
  614. }
  615. static irqreturn_t w90p910_rx_interrupt(int irq, void *dev_id)
  616. {
  617. struct net_device *dev;
  618. struct w90p910_ether *ether;
  619. struct platform_device *pdev;
  620. unsigned int status;
  621. dev = dev_id;
  622. ether = netdev_priv(dev);
  623. pdev = ether->pdev;
  624. w90p910_get_and_clear_int(dev, &status);
  625. if (status & MISTA_RDU) {
  626. netdev_rx(dev);
  627. w90p910_trigger_rx(dev);
  628. return IRQ_HANDLED;
  629. } else if (status & MISTA_RXBERR) {
  630. dev_err(&pdev->dev, "emc rx bus error\n");
  631. w90p910_reset_mac(dev);
  632. }
  633. netdev_rx(dev);
  634. return IRQ_HANDLED;
  635. }
  636. static int w90p910_ether_open(struct net_device *dev)
  637. {
  638. struct w90p910_ether *ether;
  639. struct platform_device *pdev;
  640. ether = netdev_priv(dev);
  641. pdev = ether->pdev;
  642. w90p910_reset_mac(dev);
  643. w90p910_set_fifo_threshold(dev);
  644. w90p910_set_curdest(dev);
  645. w90p910_enable_cam(dev);
  646. w90p910_enable_cam_command(dev);
  647. w90p910_enable_mac_interrupt(dev);
  648. w90p910_set_global_maccmd(dev);
  649. w90p910_enable_rx(dev, 1);
  650. clk_enable(ether->rmiiclk);
  651. clk_enable(ether->clk);
  652. ether->rx_packets = 0x0;
  653. ether->rx_bytes = 0x0;
  654. if (request_irq(ether->txirq, w90p910_tx_interrupt,
  655. 0x0, pdev->name, dev)) {
  656. dev_err(&pdev->dev, "register irq tx failed\n");
  657. return -EAGAIN;
  658. }
  659. if (request_irq(ether->rxirq, w90p910_rx_interrupt,
  660. 0x0, pdev->name, dev)) {
  661. dev_err(&pdev->dev, "register irq rx failed\n");
  662. free_irq(ether->txirq, dev);
  663. return -EAGAIN;
  664. }
  665. mod_timer(&ether->check_timer, jiffies + msecs_to_jiffies(1000));
  666. netif_start_queue(dev);
  667. w90p910_trigger_rx(dev);
  668. dev_info(&pdev->dev, "%s is OPENED\n", dev->name);
  669. return 0;
  670. }
  671. static void w90p910_ether_set_multicast_list(struct net_device *dev)
  672. {
  673. struct w90p910_ether *ether;
  674. unsigned int rx_mode;
  675. ether = netdev_priv(dev);
  676. if (dev->flags & IFF_PROMISC)
  677. rx_mode = CAMCMR_AUP | CAMCMR_AMP | CAMCMR_ABP | CAMCMR_ECMP;
  678. else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
  679. rx_mode = CAMCMR_AMP | CAMCMR_ABP | CAMCMR_ECMP;
  680. else
  681. rx_mode = CAMCMR_ECMP | CAMCMR_ABP;
  682. __raw_writel(rx_mode, ether->reg + REG_CAMCMR);
  683. }
  684. static int w90p910_ether_ioctl(struct net_device *dev,
  685. struct ifreq *ifr, int cmd)
  686. {
  687. struct w90p910_ether *ether = netdev_priv(dev);
  688. struct mii_ioctl_data *data = if_mii(ifr);
  689. return generic_mii_ioctl(&ether->mii, data, cmd, NULL);
  690. }
  691. static void w90p910_get_drvinfo(struct net_device *dev,
  692. struct ethtool_drvinfo *info)
  693. {
  694. strcpy(info->driver, DRV_MODULE_NAME);
  695. strcpy(info->version, DRV_MODULE_VERSION);
  696. }
  697. static int w90p910_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  698. {
  699. struct w90p910_ether *ether = netdev_priv(dev);
  700. return mii_ethtool_gset(&ether->mii, cmd);
  701. }
  702. static int w90p910_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  703. {
  704. struct w90p910_ether *ether = netdev_priv(dev);
  705. return mii_ethtool_sset(&ether->mii, cmd);
  706. }
  707. static int w90p910_nway_reset(struct net_device *dev)
  708. {
  709. struct w90p910_ether *ether = netdev_priv(dev);
  710. return mii_nway_restart(&ether->mii);
  711. }
  712. static u32 w90p910_get_link(struct net_device *dev)
  713. {
  714. struct w90p910_ether *ether = netdev_priv(dev);
  715. return mii_link_ok(&ether->mii);
  716. }
  717. static const struct ethtool_ops w90p910_ether_ethtool_ops = {
  718. .get_settings = w90p910_get_settings,
  719. .set_settings = w90p910_set_settings,
  720. .get_drvinfo = w90p910_get_drvinfo,
  721. .nway_reset = w90p910_nway_reset,
  722. .get_link = w90p910_get_link,
  723. };
  724. static const struct net_device_ops w90p910_ether_netdev_ops = {
  725. .ndo_open = w90p910_ether_open,
  726. .ndo_stop = w90p910_ether_close,
  727. .ndo_start_xmit = w90p910_ether_start_xmit,
  728. .ndo_get_stats = w90p910_ether_stats,
  729. .ndo_set_rx_mode = w90p910_ether_set_multicast_list,
  730. .ndo_set_mac_address = w90p910_set_mac_address,
  731. .ndo_do_ioctl = w90p910_ether_ioctl,
  732. .ndo_validate_addr = eth_validate_addr,
  733. .ndo_change_mtu = eth_change_mtu,
  734. };
  735. static void __init get_mac_address(struct net_device *dev)
  736. {
  737. struct w90p910_ether *ether = netdev_priv(dev);
  738. struct platform_device *pdev;
  739. char addr[6];
  740. pdev = ether->pdev;
  741. addr[0] = 0x00;
  742. addr[1] = 0x02;
  743. addr[2] = 0xac;
  744. addr[3] = 0x55;
  745. addr[4] = 0x88;
  746. addr[5] = 0xa8;
  747. if (is_valid_ether_addr(addr))
  748. memcpy(dev->dev_addr, &addr, 0x06);
  749. else
  750. dev_err(&pdev->dev, "invalid mac address\n");
  751. }
  752. static int w90p910_ether_setup(struct net_device *dev)
  753. {
  754. struct w90p910_ether *ether = netdev_priv(dev);
  755. ether_setup(dev);
  756. dev->netdev_ops = &w90p910_ether_netdev_ops;
  757. dev->ethtool_ops = &w90p910_ether_ethtool_ops;
  758. dev->tx_queue_len = 16;
  759. dev->dma = 0x0;
  760. dev->watchdog_timeo = TX_TIMEOUT;
  761. get_mac_address(dev);
  762. ether->cur_tx = 0x0;
  763. ether->cur_rx = 0x0;
  764. ether->finish_tx = 0x0;
  765. ether->linkflag = 0x0;
  766. ether->mii.phy_id = 0x01;
  767. ether->mii.phy_id_mask = 0x1f;
  768. ether->mii.reg_num_mask = 0x1f;
  769. ether->mii.dev = dev;
  770. ether->mii.mdio_read = w90p910_mdio_read;
  771. ether->mii.mdio_write = w90p910_mdio_write;
  772. setup_timer(&ether->check_timer, w90p910_check_link,
  773. (unsigned long)dev);
  774. return 0;
  775. }
  776. static int __devinit w90p910_ether_probe(struct platform_device *pdev)
  777. {
  778. struct w90p910_ether *ether;
  779. struct net_device *dev;
  780. int error;
  781. dev = alloc_etherdev(sizeof(struct w90p910_ether));
  782. if (!dev)
  783. return -ENOMEM;
  784. ether = netdev_priv(dev);
  785. ether->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  786. if (ether->res == NULL) {
  787. dev_err(&pdev->dev, "failed to get I/O memory\n");
  788. error = -ENXIO;
  789. goto failed_free;
  790. }
  791. if (!request_mem_region(ether->res->start,
  792. resource_size(ether->res), pdev->name)) {
  793. dev_err(&pdev->dev, "failed to request I/O memory\n");
  794. error = -EBUSY;
  795. goto failed_free;
  796. }
  797. ether->reg = ioremap(ether->res->start, resource_size(ether->res));
  798. if (ether->reg == NULL) {
  799. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  800. error = -ENXIO;
  801. goto failed_free_mem;
  802. }
  803. ether->txirq = platform_get_irq(pdev, 0);
  804. if (ether->txirq < 0) {
  805. dev_err(&pdev->dev, "failed to get ether tx irq\n");
  806. error = -ENXIO;
  807. goto failed_free_io;
  808. }
  809. ether->rxirq = platform_get_irq(pdev, 1);
  810. if (ether->rxirq < 0) {
  811. dev_err(&pdev->dev, "failed to get ether rx irq\n");
  812. error = -ENXIO;
  813. goto failed_free_txirq;
  814. }
  815. platform_set_drvdata(pdev, dev);
  816. ether->clk = clk_get(&pdev->dev, NULL);
  817. if (IS_ERR(ether->clk)) {
  818. dev_err(&pdev->dev, "failed to get ether clock\n");
  819. error = PTR_ERR(ether->clk);
  820. goto failed_free_rxirq;
  821. }
  822. ether->rmiiclk = clk_get(&pdev->dev, "RMII");
  823. if (IS_ERR(ether->rmiiclk)) {
  824. dev_err(&pdev->dev, "failed to get ether clock\n");
  825. error = PTR_ERR(ether->rmiiclk);
  826. goto failed_put_clk;
  827. }
  828. ether->pdev = pdev;
  829. w90p910_ether_setup(dev);
  830. error = register_netdev(dev);
  831. if (error != 0) {
  832. dev_err(&pdev->dev, "Regiter EMC w90p910 FAILED\n");
  833. error = -ENODEV;
  834. goto failed_put_rmiiclk;
  835. }
  836. return 0;
  837. failed_put_rmiiclk:
  838. clk_put(ether->rmiiclk);
  839. failed_put_clk:
  840. clk_put(ether->clk);
  841. failed_free_rxirq:
  842. free_irq(ether->rxirq, pdev);
  843. platform_set_drvdata(pdev, NULL);
  844. failed_free_txirq:
  845. free_irq(ether->txirq, pdev);
  846. failed_free_io:
  847. iounmap(ether->reg);
  848. failed_free_mem:
  849. release_mem_region(ether->res->start, resource_size(ether->res));
  850. failed_free:
  851. free_netdev(dev);
  852. return error;
  853. }
  854. static int __devexit w90p910_ether_remove(struct platform_device *pdev)
  855. {
  856. struct net_device *dev = platform_get_drvdata(pdev);
  857. struct w90p910_ether *ether = netdev_priv(dev);
  858. unregister_netdev(dev);
  859. clk_put(ether->rmiiclk);
  860. clk_put(ether->clk);
  861. iounmap(ether->reg);
  862. release_mem_region(ether->res->start, resource_size(ether->res));
  863. free_irq(ether->txirq, dev);
  864. free_irq(ether->rxirq, dev);
  865. del_timer_sync(&ether->check_timer);
  866. platform_set_drvdata(pdev, NULL);
  867. free_netdev(dev);
  868. return 0;
  869. }
  870. static struct platform_driver w90p910_ether_driver = {
  871. .probe = w90p910_ether_probe,
  872. .remove = __devexit_p(w90p910_ether_remove),
  873. .driver = {
  874. .name = "nuc900-emc",
  875. .owner = THIS_MODULE,
  876. },
  877. };
  878. module_platform_driver(w90p910_ether_driver);
  879. MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
  880. MODULE_DESCRIPTION("w90p910 MAC driver!");
  881. MODULE_LICENSE("GPL");
  882. MODULE_ALIAS("platform:nuc900-emc");