dm9000.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * dm9000.c: Version 1.2 03/18/2003
  3. *
  4. * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
  5. * Copyright (C) 1997 Sten Wang
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
  18. *
  19. * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
  20. * 06/22/2001 Support DM9801 progrmming
  21. * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
  22. * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
  23. * R17 = (R17 & 0xfff0) | NF + 3
  24. * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
  25. * R17 = (R17 & 0xfff0) | NF
  26. *
  27. * v1.00 modify by simon 2001.9.5
  28. * change for kernel 2.4.x
  29. *
  30. * v1.1 11/09/2001 fix force mode bug
  31. *
  32. * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
  33. * Fixed phy reset.
  34. * Added tx/rx 32 bit mode.
  35. * Cleaned up for kernel merge.
  36. *
  37. * 03/03/2004 Sascha Hauer <s.hauer@pengutronix.de>
  38. * Port to 2.6 kernel
  39. *
  40. * 24-Sep-2004 Ben Dooks <ben@simtec.co.uk>
  41. * Cleanup of code to remove ifdefs
  42. * Allowed platform device data to influence access width
  43. * Reformatting areas of code
  44. *
  45. * 17-Mar-2005 Sascha Hauer <s.hauer@pengutronix.de>
  46. * * removed 2.4 style module parameters
  47. * * removed removed unused stat counter and fixed
  48. * net_device_stats
  49. * * introduced tx_timeout function
  50. * * reworked locking
  51. *
  52. * 01-Jul-2005 Ben Dooks <ben@simtec.co.uk>
  53. * * fixed spinlock call without pointer
  54. * * ensure spinlock is initialised
  55. */
  56. #include <linux/module.h>
  57. #include <linux/ioport.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/etherdevice.h>
  60. #include <linux/init.h>
  61. #include <linux/skbuff.h>
  62. #include <linux/spinlock.h>
  63. #include <linux/crc32.h>
  64. #include <linux/mii.h>
  65. #include <linux/dm9000.h>
  66. #include <linux/delay.h>
  67. #include <linux/platform_device.h>
  68. #include <asm/delay.h>
  69. #include <asm/irq.h>
  70. #include <asm/io.h>
  71. #include "dm9000.h"
  72. /* Board/System/Debug information/definition ---------------- */
  73. #define DM9000_PHY 0x40 /* PHY address 0x01 */
  74. #define TRUE 1
  75. #define FALSE 0
  76. #define CARDNAME "dm9000"
  77. #define PFX CARDNAME ": "
  78. #define DM9000_TIMER_WUT jiffies+(HZ*2) /* timer wakeup time : 2 second */
  79. #define DM9000_DEBUG 0
  80. #if DM9000_DEBUG > 2
  81. #define PRINTK3(args...) printk(CARDNAME ": " args)
  82. #else
  83. #define PRINTK3(args...) do { } while(0)
  84. #endif
  85. #if DM9000_DEBUG > 1
  86. #define PRINTK2(args...) printk(CARDNAME ": " args)
  87. #else
  88. #define PRINTK2(args...) do { } while(0)
  89. #endif
  90. #if DM9000_DEBUG > 0
  91. #define PRINTK1(args...) printk(CARDNAME ": " args)
  92. #define PRINTK(args...) printk(CARDNAME ": " args)
  93. #else
  94. #define PRINTK1(args...) do { } while(0)
  95. #define PRINTK(args...) printk(KERN_DEBUG args)
  96. #endif
  97. /*
  98. * Transmit timeout, default 5 seconds.
  99. */
  100. static int watchdog = 5000;
  101. module_param(watchdog, int, 0400);
  102. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  103. /* Structure/enum declaration ------------------------------- */
  104. typedef struct board_info {
  105. void __iomem *io_addr; /* Register I/O base address */
  106. void __iomem *io_data; /* Data I/O address */
  107. u16 irq; /* IRQ */
  108. u16 tx_pkt_cnt;
  109. u16 queue_pkt_len;
  110. u16 queue_start_addr;
  111. u16 dbug_cnt;
  112. u8 io_mode; /* 0:word, 2:byte */
  113. u8 phy_addr;
  114. void (*inblk)(void __iomem *port, void *data, int length);
  115. void (*outblk)(void __iomem *port, void *data, int length);
  116. void (*dumpblk)(void __iomem *port, int length);
  117. struct resource *addr_res; /* resources found */
  118. struct resource *data_res;
  119. struct resource *addr_req; /* resources requested */
  120. struct resource *data_req;
  121. struct resource *irq_res;
  122. struct timer_list timer;
  123. struct net_device_stats stats;
  124. unsigned char srom[128];
  125. spinlock_t lock;
  126. struct mii_if_info mii;
  127. u32 msg_enable;
  128. } board_info_t;
  129. /* function declaration ------------------------------------- */
  130. static int dm9000_probe(struct platform_device *);
  131. static int dm9000_open(struct net_device *);
  132. static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
  133. static int dm9000_stop(struct net_device *);
  134. static void dm9000_timer(unsigned long);
  135. static void dm9000_init_dm9000(struct net_device *);
  136. static struct net_device_stats *dm9000_get_stats(struct net_device *);
  137. static irqreturn_t dm9000_interrupt(int, void *, struct pt_regs *);
  138. static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
  139. static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
  140. int value);
  141. static u16 read_srom_word(board_info_t *, int);
  142. static void dm9000_rx(struct net_device *);
  143. static void dm9000_hash_table(struct net_device *);
  144. //#define DM9000_PROGRAM_EEPROM
  145. #ifdef DM9000_PROGRAM_EEPROM
  146. static void program_eeprom(board_info_t * db);
  147. #endif
  148. /* DM9000 network board routine ---------------------------- */
  149. static void
  150. dm9000_reset(board_info_t * db)
  151. {
  152. PRINTK1("dm9000x: resetting\n");
  153. /* RESET device */
  154. writeb(DM9000_NCR, db->io_addr);
  155. udelay(200);
  156. writeb(NCR_RST, db->io_data);
  157. udelay(200);
  158. }
  159. /*
  160. * Read a byte from I/O port
  161. */
  162. static u8
  163. ior(board_info_t * db, int reg)
  164. {
  165. writeb(reg, db->io_addr);
  166. return readb(db->io_data);
  167. }
  168. /*
  169. * Write a byte to I/O port
  170. */
  171. static void
  172. iow(board_info_t * db, int reg, int value)
  173. {
  174. writeb(reg, db->io_addr);
  175. writeb(value, db->io_data);
  176. }
  177. /* routines for sending block to chip */
  178. static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
  179. {
  180. writesb(reg, data, count);
  181. }
  182. static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
  183. {
  184. writesw(reg, data, (count+1) >> 1);
  185. }
  186. static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
  187. {
  188. writesl(reg, data, (count+3) >> 2);
  189. }
  190. /* input block from chip to memory */
  191. static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
  192. {
  193. readsb(reg, data, count);
  194. }
  195. static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
  196. {
  197. readsw(reg, data, (count+1) >> 1);
  198. }
  199. static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
  200. {
  201. readsl(reg, data, (count+3) >> 2);
  202. }
  203. /* dump block from chip to null */
  204. static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
  205. {
  206. int i;
  207. int tmp;
  208. for (i = 0; i < count; i++)
  209. tmp = readb(reg);
  210. }
  211. static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
  212. {
  213. int i;
  214. int tmp;
  215. count = (count + 1) >> 1;
  216. for (i = 0; i < count; i++)
  217. tmp = readw(reg);
  218. }
  219. static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
  220. {
  221. int i;
  222. int tmp;
  223. count = (count + 3) >> 2;
  224. for (i = 0; i < count; i++)
  225. tmp = readl(reg);
  226. }
  227. /* dm9000_set_io
  228. *
  229. * select the specified set of io routines to use with the
  230. * device
  231. */
  232. static void dm9000_set_io(struct board_info *db, int byte_width)
  233. {
  234. /* use the size of the data resource to work out what IO
  235. * routines we want to use
  236. */
  237. switch (byte_width) {
  238. case 1:
  239. db->dumpblk = dm9000_dumpblk_8bit;
  240. db->outblk = dm9000_outblk_8bit;
  241. db->inblk = dm9000_inblk_8bit;
  242. break;
  243. case 2:
  244. db->dumpblk = dm9000_dumpblk_16bit;
  245. db->outblk = dm9000_outblk_16bit;
  246. db->inblk = dm9000_inblk_16bit;
  247. break;
  248. case 3:
  249. printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
  250. db->dumpblk = dm9000_dumpblk_16bit;
  251. db->outblk = dm9000_outblk_16bit;
  252. db->inblk = dm9000_inblk_16bit;
  253. break;
  254. case 4:
  255. default:
  256. db->dumpblk = dm9000_dumpblk_32bit;
  257. db->outblk = dm9000_outblk_32bit;
  258. db->inblk = dm9000_inblk_32bit;
  259. break;
  260. }
  261. }
  262. /* Our watchdog timed out. Called by the networking layer */
  263. static void dm9000_timeout(struct net_device *dev)
  264. {
  265. board_info_t *db = (board_info_t *) dev->priv;
  266. u8 reg_save;
  267. unsigned long flags;
  268. /* Save previous register address */
  269. reg_save = readb(db->io_addr);
  270. spin_lock_irqsave(&db->lock,flags);
  271. netif_stop_queue(dev);
  272. dm9000_reset(db);
  273. dm9000_init_dm9000(dev);
  274. /* We can accept TX packets again */
  275. dev->trans_start = jiffies;
  276. netif_wake_queue(dev);
  277. /* Restore previous register address */
  278. writeb(reg_save, db->io_addr);
  279. spin_unlock_irqrestore(&db->lock,flags);
  280. }
  281. /* dm9000_release_board
  282. *
  283. * release a board, and any mapped resources
  284. */
  285. static void
  286. dm9000_release_board(struct platform_device *pdev, struct board_info *db)
  287. {
  288. if (db->data_res == NULL) {
  289. if (db->addr_res != NULL)
  290. release_mem_region((unsigned long)db->io_addr, 4);
  291. return;
  292. }
  293. /* unmap our resources */
  294. iounmap(db->io_addr);
  295. iounmap(db->io_data);
  296. /* release the resources */
  297. if (db->data_req != NULL) {
  298. release_resource(db->data_req);
  299. kfree(db->data_req);
  300. }
  301. if (db->addr_res != NULL) {
  302. release_resource(db->addr_res);
  303. kfree(db->addr_req);
  304. }
  305. }
  306. #define res_size(_r) (((_r)->end - (_r)->start) + 1)
  307. /*
  308. * Search DM9000 board, allocate space and register it
  309. */
  310. static int
  311. dm9000_probe(struct platform_device *pdev)
  312. {
  313. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  314. struct board_info *db; /* Point a board information structure */
  315. struct net_device *ndev;
  316. unsigned long base;
  317. int ret = 0;
  318. int iosize;
  319. int i;
  320. u32 id_val;
  321. /* Init network device */
  322. ndev = alloc_etherdev(sizeof (struct board_info));
  323. if (!ndev) {
  324. printk("%s: could not allocate device.\n", CARDNAME);
  325. return -ENOMEM;
  326. }
  327. SET_MODULE_OWNER(ndev);
  328. SET_NETDEV_DEV(ndev, &pdev->dev);
  329. PRINTK2("dm9000_probe()");
  330. /* setup board info structure */
  331. db = (struct board_info *) ndev->priv;
  332. memset(db, 0, sizeof (*db));
  333. spin_lock_init(&db->lock);
  334. if (pdev->num_resources < 2) {
  335. ret = -ENODEV;
  336. goto out;
  337. } else if (pdev->num_resources == 2) {
  338. base = pdev->resource[0].start;
  339. if (!request_mem_region(base, 4, ndev->name)) {
  340. ret = -EBUSY;
  341. goto out;
  342. }
  343. ndev->base_addr = base;
  344. ndev->irq = pdev->resource[1].start;
  345. db->io_addr = (void __iomem *)base;
  346. db->io_data = (void __iomem *)(base + 4);
  347. } else {
  348. db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  349. db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  350. db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  351. if (db->addr_res == NULL || db->data_res == NULL ||
  352. db->irq_res == NULL) {
  353. printk(KERN_ERR PFX "insufficient resources\n");
  354. ret = -ENOENT;
  355. goto out;
  356. }
  357. i = res_size(db->addr_res);
  358. db->addr_req = request_mem_region(db->addr_res->start, i,
  359. pdev->name);
  360. if (db->addr_req == NULL) {
  361. printk(KERN_ERR PFX "cannot claim address reg area\n");
  362. ret = -EIO;
  363. goto out;
  364. }
  365. db->io_addr = ioremap(db->addr_res->start, i);
  366. if (db->io_addr == NULL) {
  367. printk(KERN_ERR "failed to ioremap address reg\n");
  368. ret = -EINVAL;
  369. goto out;
  370. }
  371. iosize = res_size(db->data_res);
  372. db->data_req = request_mem_region(db->data_res->start, iosize,
  373. pdev->name);
  374. if (db->data_req == NULL) {
  375. printk(KERN_ERR PFX "cannot claim data reg area\n");
  376. ret = -EIO;
  377. goto out;
  378. }
  379. db->io_data = ioremap(db->data_res->start, iosize);
  380. if (db->io_data == NULL) {
  381. printk(KERN_ERR "failed to ioremap data reg\n");
  382. ret = -EINVAL;
  383. goto out;
  384. }
  385. /* fill in parameters for net-dev structure */
  386. ndev->base_addr = (unsigned long)db->io_addr;
  387. ndev->irq = db->irq_res->start;
  388. /* ensure at least we have a default set of IO routines */
  389. dm9000_set_io(db, iosize);
  390. }
  391. /* check to see if anything is being over-ridden */
  392. if (pdata != NULL) {
  393. /* check to see if the driver wants to over-ride the
  394. * default IO width */
  395. if (pdata->flags & DM9000_PLATF_8BITONLY)
  396. dm9000_set_io(db, 1);
  397. if (pdata->flags & DM9000_PLATF_16BITONLY)
  398. dm9000_set_io(db, 2);
  399. if (pdata->flags & DM9000_PLATF_32BITONLY)
  400. dm9000_set_io(db, 4);
  401. /* check to see if there are any IO routine
  402. * over-rides */
  403. if (pdata->inblk != NULL)
  404. db->inblk = pdata->inblk;
  405. if (pdata->outblk != NULL)
  406. db->outblk = pdata->outblk;
  407. if (pdata->dumpblk != NULL)
  408. db->dumpblk = pdata->dumpblk;
  409. }
  410. dm9000_reset(db);
  411. /* try two times, DM9000 sometimes gets the first read wrong */
  412. for (i = 0; i < 2; i++) {
  413. id_val = ior(db, DM9000_VIDL);
  414. id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  415. id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  416. id_val |= (u32)ior(db, DM9000_PIDH) << 24;
  417. if (id_val == DM9000_ID)
  418. break;
  419. printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
  420. }
  421. if (id_val != DM9000_ID) {
  422. printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
  423. goto release;
  424. }
  425. /* from this point we assume that we have found a DM9000 */
  426. /* driver system function */
  427. ether_setup(ndev);
  428. ndev->open = &dm9000_open;
  429. ndev->hard_start_xmit = &dm9000_start_xmit;
  430. ndev->tx_timeout = &dm9000_timeout;
  431. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  432. ndev->stop = &dm9000_stop;
  433. ndev->get_stats = &dm9000_get_stats;
  434. ndev->set_multicast_list = &dm9000_hash_table;
  435. #ifdef DM9000_PROGRAM_EEPROM
  436. program_eeprom(db);
  437. #endif
  438. db->msg_enable = NETIF_MSG_LINK;
  439. db->mii.phy_id_mask = 0x1f;
  440. db->mii.reg_num_mask = 0x1f;
  441. db->mii.force_media = 0;
  442. db->mii.full_duplex = 0;
  443. db->mii.dev = ndev;
  444. db->mii.mdio_read = dm9000_phy_read;
  445. db->mii.mdio_write = dm9000_phy_write;
  446. /* Read SROM content */
  447. for (i = 0; i < 64; i++)
  448. ((u16 *) db->srom)[i] = read_srom_word(db, i);
  449. /* Set Node Address */
  450. for (i = 0; i < 6; i++)
  451. ndev->dev_addr[i] = db->srom[i];
  452. if (!is_valid_ether_addr(ndev->dev_addr))
  453. printk("%s: Invalid ethernet MAC address. Please "
  454. "set using ifconfig\n", ndev->name);
  455. platform_set_drvdata(pdev, ndev);
  456. ret = register_netdev(ndev);
  457. if (ret == 0) {
  458. printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
  459. ndev->name, db->io_addr, db->io_data, ndev->irq);
  460. for (i = 0; i < 5; i++)
  461. printk("%02x:", ndev->dev_addr[i]);
  462. printk("%02x\n", ndev->dev_addr[5]);
  463. }
  464. return 0;
  465. release:
  466. out:
  467. printk("%s: not found (%d).\n", CARDNAME, ret);
  468. dm9000_release_board(pdev, db);
  469. kfree(ndev);
  470. return ret;
  471. }
  472. /*
  473. * Open the interface.
  474. * The interface is opened whenever "ifconfig" actives it.
  475. */
  476. static int
  477. dm9000_open(struct net_device *dev)
  478. {
  479. board_info_t *db = (board_info_t *) dev->priv;
  480. PRINTK2("entering dm9000_open\n");
  481. if (request_irq(dev->irq, &dm9000_interrupt, SA_SHIRQ, dev->name, dev))
  482. return -EAGAIN;
  483. /* Initialize DM9000 board */
  484. dm9000_reset(db);
  485. dm9000_init_dm9000(dev);
  486. /* Init driver variable */
  487. db->dbug_cnt = 0;
  488. /* set and active a timer process */
  489. init_timer(&db->timer);
  490. db->timer.expires = DM9000_TIMER_WUT;
  491. db->timer.data = (unsigned long) dev;
  492. db->timer.function = &dm9000_timer;
  493. add_timer(&db->timer);
  494. mii_check_media(&db->mii, netif_msg_link(db), 1);
  495. netif_start_queue(dev);
  496. return 0;
  497. }
  498. /*
  499. * Initilize dm9000 board
  500. */
  501. static void
  502. dm9000_init_dm9000(struct net_device *dev)
  503. {
  504. board_info_t *db = (board_info_t *) dev->priv;
  505. PRINTK1("entering %s\n",__FUNCTION__);
  506. /* I/O mode */
  507. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
  508. /* GPIO0 on pre-activate PHY */
  509. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  510. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  511. iow(db, DM9000_GPR, 0); /* Enable PHY */
  512. /* Program operating register */
  513. iow(db, DM9000_TCR, 0); /* TX Polling clear */
  514. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  515. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  516. iow(db, DM9000_SMCR, 0); /* Special Mode */
  517. /* clear TX status */
  518. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  519. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
  520. /* Set address filter table */
  521. dm9000_hash_table(dev);
  522. /* Activate DM9000 */
  523. iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  524. /* Enable TX/RX interrupt mask */
  525. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  526. /* Init Driver variable */
  527. db->tx_pkt_cnt = 0;
  528. db->queue_pkt_len = 0;
  529. dev->trans_start = 0;
  530. spin_lock_init(&db->lock);
  531. }
  532. /*
  533. * Hardware start transmission.
  534. * Send a packet to media from the upper layer.
  535. */
  536. static int
  537. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  538. {
  539. board_info_t *db = (board_info_t *) dev->priv;
  540. PRINTK3("dm9000_start_xmit\n");
  541. if (db->tx_pkt_cnt > 1)
  542. return 1;
  543. netif_stop_queue(dev);
  544. /* Disable all interrupts */
  545. iow(db, DM9000_IMR, IMR_PAR);
  546. /* Move data to DM9000 TX RAM */
  547. writeb(DM9000_MWCMD, db->io_addr);
  548. (db->outblk)(db->io_data, skb->data, skb->len);
  549. db->stats.tx_bytes += skb->len;
  550. /* TX control: First packet immediately send, second packet queue */
  551. if (db->tx_pkt_cnt == 0) {
  552. /* First Packet */
  553. db->tx_pkt_cnt++;
  554. /* Set TX length to DM9000 */
  555. iow(db, DM9000_TXPLL, skb->len & 0xff);
  556. iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
  557. /* Issue TX polling command */
  558. iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  559. dev->trans_start = jiffies; /* save the time stamp */
  560. } else {
  561. /* Second packet */
  562. db->tx_pkt_cnt++;
  563. db->queue_pkt_len = skb->len;
  564. }
  565. /* free this SKB */
  566. dev_kfree_skb(skb);
  567. /* Re-enable resource check */
  568. if (db->tx_pkt_cnt == 1)
  569. netif_wake_queue(dev);
  570. /* Re-enable interrupt */
  571. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  572. return 0;
  573. }
  574. static void
  575. dm9000_shutdown(struct net_device *dev)
  576. {
  577. board_info_t *db = (board_info_t *) dev->priv;
  578. /* RESET device */
  579. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  580. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  581. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  582. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  583. }
  584. /*
  585. * Stop the interface.
  586. * The interface is stopped when it is brought.
  587. */
  588. static int
  589. dm9000_stop(struct net_device *ndev)
  590. {
  591. board_info_t *db = (board_info_t *) ndev->priv;
  592. PRINTK1("entering %s\n",__FUNCTION__);
  593. /* deleted timer */
  594. del_timer(&db->timer);
  595. netif_stop_queue(ndev);
  596. netif_carrier_off(ndev);
  597. /* free interrupt */
  598. free_irq(ndev->irq, ndev);
  599. dm9000_shutdown(ndev);
  600. return 0;
  601. }
  602. /*
  603. * DM9000 interrupt handler
  604. * receive the packet to upper layer, free the transmitted packet
  605. */
  606. void
  607. dm9000_tx_done(struct net_device *dev, board_info_t * db)
  608. {
  609. int tx_status = ior(db, DM9000_NSR); /* Got TX status */
  610. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  611. /* One packet sent complete */
  612. db->tx_pkt_cnt--;
  613. db->stats.tx_packets++;
  614. /* Queue packet check & send */
  615. if (db->tx_pkt_cnt > 0) {
  616. iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
  617. iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
  618. iow(db, DM9000_TCR, TCR_TXREQ);
  619. dev->trans_start = jiffies;
  620. }
  621. netif_wake_queue(dev);
  622. }
  623. }
  624. static irqreturn_t
  625. dm9000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  626. {
  627. struct net_device *dev = dev_id;
  628. board_info_t *db;
  629. int int_status;
  630. u8 reg_save;
  631. PRINTK3("entering %s\n",__FUNCTION__);
  632. if (!dev) {
  633. PRINTK1("dm9000_interrupt() without DEVICE arg\n");
  634. return IRQ_HANDLED;
  635. }
  636. /* A real interrupt coming */
  637. db = (board_info_t *) dev->priv;
  638. spin_lock(&db->lock);
  639. /* Save previous register address */
  640. reg_save = readb(db->io_addr);
  641. /* Disable all interrupts */
  642. iow(db, DM9000_IMR, IMR_PAR);
  643. /* Got DM9000 interrupt status */
  644. int_status = ior(db, DM9000_ISR); /* Got ISR */
  645. iow(db, DM9000_ISR, int_status); /* Clear ISR status */
  646. /* Received the coming packet */
  647. if (int_status & ISR_PRS)
  648. dm9000_rx(dev);
  649. /* Trnasmit Interrupt check */
  650. if (int_status & ISR_PTS)
  651. dm9000_tx_done(dev, db);
  652. /* Re-enable interrupt mask */
  653. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  654. /* Restore previous register address */
  655. writeb(reg_save, db->io_addr);
  656. spin_unlock(&db->lock);
  657. return IRQ_HANDLED;
  658. }
  659. /*
  660. * Get statistics from driver.
  661. */
  662. static struct net_device_stats *
  663. dm9000_get_stats(struct net_device *dev)
  664. {
  665. board_info_t *db = (board_info_t *) dev->priv;
  666. return &db->stats;
  667. }
  668. /*
  669. * A periodic timer routine
  670. * Dynamic media sense, allocated Rx buffer...
  671. */
  672. static void
  673. dm9000_timer(unsigned long data)
  674. {
  675. struct net_device *dev = (struct net_device *) data;
  676. board_info_t *db = (board_info_t *) dev->priv;
  677. PRINTK3("dm9000_timer()\n");
  678. mii_check_media(&db->mii, netif_msg_link(db), 0);
  679. /* Set timer again */
  680. db->timer.expires = DM9000_TIMER_WUT;
  681. add_timer(&db->timer);
  682. }
  683. struct dm9000_rxhdr {
  684. u16 RxStatus;
  685. u16 RxLen;
  686. } __attribute__((__packed__));
  687. /*
  688. * Received a packet and pass to upper layer
  689. */
  690. static void
  691. dm9000_rx(struct net_device *dev)
  692. {
  693. board_info_t *db = (board_info_t *) dev->priv;
  694. struct dm9000_rxhdr rxhdr;
  695. struct sk_buff *skb;
  696. u8 rxbyte, *rdptr;
  697. int GoodPacket;
  698. int RxLen;
  699. /* Check packet ready or not */
  700. do {
  701. ior(db, DM9000_MRCMDX); /* Dummy read */
  702. /* Get most updated data */
  703. rxbyte = readb(db->io_data);
  704. /* Status check: this byte must be 0 or 1 */
  705. if (rxbyte > DM9000_PKT_RDY) {
  706. printk("status check failed: %d\n", rxbyte);
  707. iow(db, DM9000_RCR, 0x00); /* Stop Device */
  708. iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
  709. return;
  710. }
  711. if (rxbyte != DM9000_PKT_RDY)
  712. return;
  713. /* A packet ready now & Get status/length */
  714. GoodPacket = TRUE;
  715. writeb(DM9000_MRCMD, db->io_addr);
  716. (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
  717. RxLen = rxhdr.RxLen;
  718. /* Packet Status check */
  719. if (RxLen < 0x40) {
  720. GoodPacket = FALSE;
  721. PRINTK1("Bad Packet received (runt)\n");
  722. }
  723. if (RxLen > DM9000_PKT_MAX) {
  724. PRINTK1("RST: RX Len:%x\n", RxLen);
  725. }
  726. if (rxhdr.RxStatus & 0xbf00) {
  727. GoodPacket = FALSE;
  728. if (rxhdr.RxStatus & 0x100) {
  729. PRINTK1("fifo error\n");
  730. db->stats.rx_fifo_errors++;
  731. }
  732. if (rxhdr.RxStatus & 0x200) {
  733. PRINTK1("crc error\n");
  734. db->stats.rx_crc_errors++;
  735. }
  736. if (rxhdr.RxStatus & 0x8000) {
  737. PRINTK1("length error\n");
  738. db->stats.rx_length_errors++;
  739. }
  740. }
  741. /* Move data from DM9000 */
  742. if (GoodPacket
  743. && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
  744. skb->dev = dev;
  745. skb_reserve(skb, 2);
  746. rdptr = (u8 *) skb_put(skb, RxLen - 4);
  747. /* Read received packet from RX SRAM */
  748. (db->inblk)(db->io_data, rdptr, RxLen);
  749. db->stats.rx_bytes += RxLen;
  750. /* Pass to upper layer */
  751. skb->protocol = eth_type_trans(skb, dev);
  752. netif_rx(skb);
  753. db->stats.rx_packets++;
  754. } else {
  755. /* need to dump the packet's data */
  756. (db->dumpblk)(db->io_data, RxLen);
  757. }
  758. } while (rxbyte == DM9000_PKT_RDY);
  759. }
  760. /*
  761. * Read a word data from SROM
  762. */
  763. static u16
  764. read_srom_word(board_info_t * db, int offset)
  765. {
  766. iow(db, DM9000_EPAR, offset);
  767. iow(db, DM9000_EPCR, EPCR_ERPRR);
  768. mdelay(8); /* according to the datasheet 200us should be enough,
  769. but it doesn't work */
  770. iow(db, DM9000_EPCR, 0x0);
  771. return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
  772. }
  773. #ifdef DM9000_PROGRAM_EEPROM
  774. /*
  775. * Write a word data to SROM
  776. */
  777. static void
  778. write_srom_word(board_info_t * db, int offset, u16 val)
  779. {
  780. iow(db, DM9000_EPAR, offset);
  781. iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
  782. iow(db, DM9000_EPDRL, (val & 0xff));
  783. iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
  784. mdelay(8); /* same shit */
  785. iow(db, DM9000_EPCR, 0);
  786. }
  787. /*
  788. * Only for development:
  789. * Here we write static data to the eeprom in case
  790. * we don't have valid content on a new board
  791. */
  792. static void
  793. program_eeprom(board_info_t * db)
  794. {
  795. u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
  796. 0x0000, /* Autoload: accept nothing */
  797. 0x0a46, 0x9000, /* Vendor / Product ID */
  798. 0x0000, /* pin control */
  799. 0x0000,
  800. }; /* Wake-up mode control */
  801. int i;
  802. for (i = 0; i < 8; i++)
  803. write_srom_word(db, i, eeprom[i]);
  804. }
  805. #endif
  806. /*
  807. * Calculate the CRC valude of the Rx packet
  808. * flag = 1 : return the reverse CRC (for the received packet CRC)
  809. * 0 : return the normal CRC (for Hash Table index)
  810. */
  811. static unsigned long
  812. cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
  813. {
  814. u32 crc = ether_crc_le(Len, Data);
  815. if (flag)
  816. return ~crc;
  817. return crc;
  818. }
  819. /*
  820. * Set DM9000 multicast address
  821. */
  822. static void
  823. dm9000_hash_table(struct net_device *dev)
  824. {
  825. board_info_t *db = (board_info_t *) dev->priv;
  826. struct dev_mc_list *mcptr = dev->mc_list;
  827. int mc_cnt = dev->mc_count;
  828. u32 hash_val;
  829. u16 i, oft, hash_table[4];
  830. unsigned long flags;
  831. PRINTK2("dm9000_hash_table()\n");
  832. spin_lock_irqsave(&db->lock,flags);
  833. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  834. iow(db, oft, dev->dev_addr[i]);
  835. /* Clear Hash Table */
  836. for (i = 0; i < 4; i++)
  837. hash_table[i] = 0x0;
  838. /* broadcast address */
  839. hash_table[3] = 0x8000;
  840. /* the multicast address in Hash Table : 64 bits */
  841. for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
  842. hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
  843. hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
  844. }
  845. /* Write the hash table to MAC MD table */
  846. for (i = 0, oft = 0x16; i < 4; i++) {
  847. iow(db, oft++, hash_table[i] & 0xff);
  848. iow(db, oft++, (hash_table[i] >> 8) & 0xff);
  849. }
  850. spin_unlock_irqrestore(&db->lock,flags);
  851. }
  852. /*
  853. * Read a word from phyxcer
  854. */
  855. static int
  856. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  857. {
  858. board_info_t *db = (board_info_t *) dev->priv;
  859. unsigned long flags;
  860. unsigned int reg_save;
  861. int ret;
  862. spin_lock_irqsave(&db->lock,flags);
  863. /* Save previous register address */
  864. reg_save = readb(db->io_addr);
  865. /* Fill the phyxcer register into REG_0C */
  866. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  867. iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  868. udelay(100); /* Wait read complete */
  869. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  870. /* The read data keeps on REG_0D & REG_0E */
  871. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
  872. /* restore the previous address */
  873. writeb(reg_save, db->io_addr);
  874. spin_unlock_irqrestore(&db->lock,flags);
  875. return ret;
  876. }
  877. /*
  878. * Write a word to phyxcer
  879. */
  880. static void
  881. dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
  882. {
  883. board_info_t *db = (board_info_t *) dev->priv;
  884. unsigned long flags;
  885. unsigned long reg_save;
  886. spin_lock_irqsave(&db->lock,flags);
  887. /* Save previous register address */
  888. reg_save = readb(db->io_addr);
  889. /* Fill the phyxcer register into REG_0C */
  890. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  891. /* Fill the written data into REG_0D & REG_0E */
  892. iow(db, DM9000_EPDRL, (value & 0xff));
  893. iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
  894. iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  895. udelay(500); /* Wait write complete */
  896. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  897. /* restore the previous address */
  898. writeb(reg_save, db->io_addr);
  899. spin_unlock_irqrestore(&db->lock,flags);
  900. }
  901. static int
  902. dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
  903. {
  904. struct net_device *ndev = platform_get_drvdata(dev);
  905. if (ndev) {
  906. if (netif_running(ndev)) {
  907. netif_device_detach(ndev);
  908. dm9000_shutdown(ndev);
  909. }
  910. }
  911. return 0;
  912. }
  913. static int
  914. dm9000_drv_resume(struct platform_device *dev)
  915. {
  916. struct net_device *ndev = platform_get_drvdata(dev);
  917. board_info_t *db = (board_info_t *) ndev->priv;
  918. if (ndev) {
  919. if (netif_running(ndev)) {
  920. dm9000_reset(db);
  921. dm9000_init_dm9000(ndev);
  922. netif_device_attach(ndev);
  923. }
  924. }
  925. return 0;
  926. }
  927. static int
  928. dm9000_drv_remove(struct platform_device *pdev)
  929. {
  930. struct net_device *ndev = platform_get_drvdata(pdev);
  931. platform_set_drvdata(pdev, NULL);
  932. unregister_netdev(ndev);
  933. dm9000_release_board(pdev, (board_info_t *) ndev->priv);
  934. kfree(ndev); /* free device structure */
  935. PRINTK1("clean_module() exit\n");
  936. return 0;
  937. }
  938. static struct platform_driver dm9000_driver = {
  939. .probe = dm9000_probe,
  940. .remove = dm9000_drv_remove,
  941. .suspend = dm9000_drv_suspend,
  942. .resume = dm9000_drv_resume,
  943. .driver = {
  944. .name = "dm9000",
  945. },
  946. };
  947. static int __init
  948. dm9000_init(void)
  949. {
  950. printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
  951. return platform_driver_register(&dm9000_driver); /* search board and register */
  952. }
  953. static void __exit
  954. dm9000_cleanup(void)
  955. {
  956. platform_driver_unregister(&dm9000_driver);
  957. }
  958. module_init(dm9000_init);
  959. module_exit(dm9000_cleanup);
  960. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  961. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  962. MODULE_LICENSE("GPL");