dm9000.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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/ethtool.h>
  66. #include <linux/dm9000.h>
  67. #include <linux/delay.h>
  68. #include <linux/platform_device.h>
  69. #include <linux/irq.h>
  70. #include <asm/delay.h>
  71. #include <asm/irq.h>
  72. #include <asm/io.h>
  73. #include "dm9000.h"
  74. /* Board/System/Debug information/definition ---------------- */
  75. #define DM9000_PHY 0x40 /* PHY address 0x01 */
  76. #define CARDNAME "dm9000"
  77. #define PFX CARDNAME ": "
  78. #define DRV_VERSION "1.30"
  79. #ifdef CONFIG_BLACKFIN
  80. #define readsb insb
  81. #define readsw insw
  82. #define readsl insl
  83. #define writesb outsb
  84. #define writesw outsw
  85. #define writesl outsl
  86. #define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH
  87. #else
  88. #define DEFAULT_TRIGGER (0)
  89. #endif
  90. /*
  91. * Transmit timeout, default 5 seconds.
  92. */
  93. static int watchdog = 5000;
  94. module_param(watchdog, int, 0400);
  95. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
  96. /* Structure/enum declaration ------------------------------- */
  97. typedef struct board_info {
  98. void __iomem *io_addr; /* Register I/O base address */
  99. void __iomem *io_data; /* Data I/O address */
  100. u16 irq; /* IRQ */
  101. u16 tx_pkt_cnt;
  102. u16 queue_pkt_len;
  103. u16 queue_start_addr;
  104. u16 dbug_cnt;
  105. u8 io_mode; /* 0:word, 2:byte */
  106. u8 phy_addr;
  107. unsigned int flags;
  108. unsigned int in_suspend :1;
  109. int debug_level;
  110. void (*inblk)(void __iomem *port, void *data, int length);
  111. void (*outblk)(void __iomem *port, void *data, int length);
  112. void (*dumpblk)(void __iomem *port, int length);
  113. struct device *dev; /* parent device */
  114. struct resource *addr_res; /* resources found */
  115. struct resource *data_res;
  116. struct resource *addr_req; /* resources requested */
  117. struct resource *data_req;
  118. struct resource *irq_res;
  119. spinlock_t lock;
  120. struct mii_if_info mii;
  121. u32 msg_enable;
  122. } board_info_t;
  123. /* debug code */
  124. #define dm9000_dbg(db, lev, msg...) do { \
  125. if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \
  126. (lev) < db->debug_level) { \
  127. dev_dbg(db->dev, msg); \
  128. } \
  129. } while (0)
  130. static inline board_info_t *to_dm9000_board(struct net_device *dev)
  131. {
  132. return dev->priv;
  133. }
  134. /* function declaration ------------------------------------- */
  135. static int dm9000_probe(struct platform_device *);
  136. static int dm9000_open(struct net_device *);
  137. static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
  138. static int dm9000_stop(struct net_device *);
  139. static void dm9000_init_dm9000(struct net_device *);
  140. static irqreturn_t dm9000_interrupt(int, void *);
  141. static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
  142. static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
  143. int value);
  144. static void dm9000_read_eeprom(board_info_t *, int addr, unsigned char *to);
  145. static void dm9000_rx(struct net_device *);
  146. static void dm9000_hash_table(struct net_device *);
  147. //#define DM9000_PROGRAM_EEPROM
  148. #ifdef DM9000_PROGRAM_EEPROM
  149. static void program_eeprom(board_info_t * db);
  150. #endif
  151. /* DM9000 network board routine ---------------------------- */
  152. static void
  153. dm9000_reset(board_info_t * db)
  154. {
  155. dev_dbg(db->dev, "resetting device\n");
  156. /* RESET device */
  157. writeb(DM9000_NCR, db->io_addr);
  158. udelay(200);
  159. writeb(NCR_RST, db->io_data);
  160. udelay(200);
  161. }
  162. /*
  163. * Read a byte from I/O port
  164. */
  165. static u8
  166. ior(board_info_t * db, int reg)
  167. {
  168. writeb(reg, db->io_addr);
  169. return readb(db->io_data);
  170. }
  171. /*
  172. * Write a byte to I/O port
  173. */
  174. static void
  175. iow(board_info_t * db, int reg, int value)
  176. {
  177. writeb(reg, db->io_addr);
  178. writeb(value, db->io_data);
  179. }
  180. /* routines for sending block to chip */
  181. static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
  182. {
  183. writesb(reg, data, count);
  184. }
  185. static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
  186. {
  187. writesw(reg, data, (count+1) >> 1);
  188. }
  189. static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
  190. {
  191. writesl(reg, data, (count+3) >> 2);
  192. }
  193. /* input block from chip to memory */
  194. static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
  195. {
  196. readsb(reg, data, count);
  197. }
  198. static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
  199. {
  200. readsw(reg, data, (count+1) >> 1);
  201. }
  202. static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
  203. {
  204. readsl(reg, data, (count+3) >> 2);
  205. }
  206. /* dump block from chip to null */
  207. static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
  208. {
  209. int i;
  210. int tmp;
  211. for (i = 0; i < count; i++)
  212. tmp = readb(reg);
  213. }
  214. static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
  215. {
  216. int i;
  217. int tmp;
  218. count = (count + 1) >> 1;
  219. for (i = 0; i < count; i++)
  220. tmp = readw(reg);
  221. }
  222. static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
  223. {
  224. int i;
  225. int tmp;
  226. count = (count + 3) >> 2;
  227. for (i = 0; i < count; i++)
  228. tmp = readl(reg);
  229. }
  230. /* dm9000_set_io
  231. *
  232. * select the specified set of io routines to use with the
  233. * device
  234. */
  235. static void dm9000_set_io(struct board_info *db, int byte_width)
  236. {
  237. /* use the size of the data resource to work out what IO
  238. * routines we want to use
  239. */
  240. switch (byte_width) {
  241. case 1:
  242. db->dumpblk = dm9000_dumpblk_8bit;
  243. db->outblk = dm9000_outblk_8bit;
  244. db->inblk = dm9000_inblk_8bit;
  245. break;
  246. case 3:
  247. dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
  248. case 2:
  249. db->dumpblk = dm9000_dumpblk_16bit;
  250. db->outblk = dm9000_outblk_16bit;
  251. db->inblk = dm9000_inblk_16bit;
  252. break;
  253. case 4:
  254. default:
  255. db->dumpblk = dm9000_dumpblk_32bit;
  256. db->outblk = dm9000_outblk_32bit;
  257. db->inblk = dm9000_inblk_32bit;
  258. break;
  259. }
  260. }
  261. /* Our watchdog timed out. Called by the networking layer */
  262. static void dm9000_timeout(struct net_device *dev)
  263. {
  264. board_info_t *db = (board_info_t *) dev->priv;
  265. u8 reg_save;
  266. unsigned long flags;
  267. /* Save previous register address */
  268. reg_save = readb(db->io_addr);
  269. spin_lock_irqsave(&db->lock,flags);
  270. netif_stop_queue(dev);
  271. dm9000_reset(db);
  272. dm9000_init_dm9000(dev);
  273. /* We can accept TX packets again */
  274. dev->trans_start = jiffies;
  275. netif_wake_queue(dev);
  276. /* Restore previous register address */
  277. writeb(reg_save, db->io_addr);
  278. spin_unlock_irqrestore(&db->lock,flags);
  279. }
  280. #ifdef CONFIG_NET_POLL_CONTROLLER
  281. /*
  282. *Used by netconsole
  283. */
  284. static void dm9000_poll_controller(struct net_device *dev)
  285. {
  286. disable_irq(dev->irq);
  287. dm9000_interrupt(dev->irq,dev);
  288. enable_irq(dev->irq);
  289. }
  290. #endif
  291. /* ethtool ops */
  292. static void dm9000_get_drvinfo(struct net_device *dev,
  293. struct ethtool_drvinfo *info)
  294. {
  295. board_info_t *dm = to_dm9000_board(dev);
  296. strcpy(info->driver, CARDNAME);
  297. strcpy(info->version, DRV_VERSION);
  298. strcpy(info->bus_info, to_platform_device(dm->dev)->name);
  299. }
  300. static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  301. {
  302. board_info_t *dm = to_dm9000_board(dev);
  303. unsigned long flags;
  304. spin_lock_irqsave(&dm->lock, flags);
  305. mii_ethtool_gset(&dm->mii, cmd);
  306. spin_lock_irqsave(&dm->lock, flags);
  307. return 0;
  308. }
  309. static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  310. {
  311. board_info_t *dm = to_dm9000_board(dev);
  312. unsigned long flags;
  313. int rc;
  314. spin_lock_irqsave(&dm->lock, flags);
  315. rc = mii_ethtool_sset(&dm->mii, cmd);
  316. spin_lock_irqsave(&dm->lock, flags);
  317. return rc;
  318. }
  319. static int dm9000_nway_reset(struct net_device *dev)
  320. {
  321. board_info_t *dm = to_dm9000_board(dev);
  322. return mii_nway_restart(&dm->mii);
  323. }
  324. static u32 dm9000_get_link(struct net_device *dev)
  325. {
  326. board_info_t *dm = to_dm9000_board(dev);
  327. return mii_link_ok(&dm->mii);
  328. }
  329. static const struct ethtool_ops dm9000_ethtool_ops = {
  330. .get_drvinfo = dm9000_get_drvinfo,
  331. .get_settings = dm9000_get_settings,
  332. .set_settings = dm9000_set_settings,
  333. .nway_reset = dm9000_nway_reset,
  334. .get_link = dm9000_get_link,
  335. };
  336. /* dm9000_release_board
  337. *
  338. * release a board, and any mapped resources
  339. */
  340. static void
  341. dm9000_release_board(struct platform_device *pdev, struct board_info *db)
  342. {
  343. if (db->data_res == NULL) {
  344. if (db->addr_res != NULL)
  345. release_mem_region((unsigned long)db->io_addr, 4);
  346. return;
  347. }
  348. /* unmap our resources */
  349. iounmap(db->io_addr);
  350. iounmap(db->io_data);
  351. /* release the resources */
  352. if (db->data_req != NULL) {
  353. release_resource(db->data_req);
  354. kfree(db->data_req);
  355. }
  356. if (db->addr_req != NULL) {
  357. release_resource(db->addr_req);
  358. kfree(db->addr_req);
  359. }
  360. }
  361. #define res_size(_r) (((_r)->end - (_r)->start) + 1)
  362. /*
  363. * Search DM9000 board, allocate space and register it
  364. */
  365. static int
  366. dm9000_probe(struct platform_device *pdev)
  367. {
  368. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  369. struct board_info *db; /* Point a board information structure */
  370. struct net_device *ndev;
  371. unsigned long base;
  372. int ret = 0;
  373. int iosize;
  374. int i;
  375. u32 id_val;
  376. /* Init network device */
  377. ndev = alloc_etherdev(sizeof (struct board_info));
  378. if (!ndev) {
  379. dev_err(&pdev->dev, "could not allocate device.\n");
  380. return -ENOMEM;
  381. }
  382. SET_NETDEV_DEV(ndev, &pdev->dev);
  383. dev_dbg(&pdev->dev, "dm9000_probe()");
  384. /* setup board info structure */
  385. db = (struct board_info *) ndev->priv;
  386. memset(db, 0, sizeof (*db));
  387. db->dev = &pdev->dev;
  388. spin_lock_init(&db->lock);
  389. if (pdev->num_resources < 2) {
  390. ret = -ENODEV;
  391. goto out;
  392. } else if (pdev->num_resources == 2) {
  393. base = pdev->resource[0].start;
  394. if (!request_mem_region(base, 4, ndev->name)) {
  395. ret = -EBUSY;
  396. goto out;
  397. }
  398. ndev->base_addr = base;
  399. ndev->irq = pdev->resource[1].start;
  400. db->io_addr = (void __iomem *)base;
  401. db->io_data = (void __iomem *)(base + 4);
  402. /* ensure at least we have a default set of IO routines */
  403. dm9000_set_io(db, 2);
  404. } else {
  405. db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  406. db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  407. db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  408. if (db->addr_res == NULL || db->data_res == NULL ||
  409. db->irq_res == NULL) {
  410. dev_err(db->dev, "insufficient resources\n");
  411. ret = -ENOENT;
  412. goto out;
  413. }
  414. i = res_size(db->addr_res);
  415. db->addr_req = request_mem_region(db->addr_res->start, i,
  416. pdev->name);
  417. if (db->addr_req == NULL) {
  418. dev_err(db->dev, "cannot claim address reg area\n");
  419. ret = -EIO;
  420. goto out;
  421. }
  422. db->io_addr = ioremap(db->addr_res->start, i);
  423. if (db->io_addr == NULL) {
  424. dev_err(db->dev, "failed to ioremap address reg\n");
  425. ret = -EINVAL;
  426. goto out;
  427. }
  428. iosize = res_size(db->data_res);
  429. db->data_req = request_mem_region(db->data_res->start, iosize,
  430. pdev->name);
  431. if (db->data_req == NULL) {
  432. dev_err(db->dev, "cannot claim data reg area\n");
  433. ret = -EIO;
  434. goto out;
  435. }
  436. db->io_data = ioremap(db->data_res->start, iosize);
  437. if (db->io_data == NULL) {
  438. dev_err(db->dev,"failed to ioremap data reg\n");
  439. ret = -EINVAL;
  440. goto out;
  441. }
  442. /* fill in parameters for net-dev structure */
  443. ndev->base_addr = (unsigned long)db->io_addr;
  444. ndev->irq = db->irq_res->start;
  445. /* ensure at least we have a default set of IO routines */
  446. dm9000_set_io(db, iosize);
  447. }
  448. /* check to see if anything is being over-ridden */
  449. if (pdata != NULL) {
  450. /* check to see if the driver wants to over-ride the
  451. * default IO width */
  452. if (pdata->flags & DM9000_PLATF_8BITONLY)
  453. dm9000_set_io(db, 1);
  454. if (pdata->flags & DM9000_PLATF_16BITONLY)
  455. dm9000_set_io(db, 2);
  456. if (pdata->flags & DM9000_PLATF_32BITONLY)
  457. dm9000_set_io(db, 4);
  458. /* check to see if there are any IO routine
  459. * over-rides */
  460. if (pdata->inblk != NULL)
  461. db->inblk = pdata->inblk;
  462. if (pdata->outblk != NULL)
  463. db->outblk = pdata->outblk;
  464. if (pdata->dumpblk != NULL)
  465. db->dumpblk = pdata->dumpblk;
  466. db->flags = pdata->flags;
  467. }
  468. dm9000_reset(db);
  469. /* try two times, DM9000 sometimes gets the first read wrong */
  470. for (i = 0; i < 2; i++) {
  471. id_val = ior(db, DM9000_VIDL);
  472. id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  473. id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  474. id_val |= (u32)ior(db, DM9000_PIDH) << 24;
  475. if (id_val == DM9000_ID)
  476. break;
  477. dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
  478. }
  479. if (id_val != DM9000_ID) {
  480. dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
  481. ret = -ENODEV;
  482. goto out;
  483. }
  484. /* from this point we assume that we have found a DM9000 */
  485. /* driver system function */
  486. ether_setup(ndev);
  487. ndev->open = &dm9000_open;
  488. ndev->hard_start_xmit = &dm9000_start_xmit;
  489. ndev->tx_timeout = &dm9000_timeout;
  490. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  491. ndev->stop = &dm9000_stop;
  492. ndev->set_multicast_list = &dm9000_hash_table;
  493. ndev->ethtool_ops = &dm9000_ethtool_ops;
  494. #ifdef CONFIG_NET_POLL_CONTROLLER
  495. ndev->poll_controller = &dm9000_poll_controller;
  496. #endif
  497. #ifdef DM9000_PROGRAM_EEPROM
  498. program_eeprom(db);
  499. #endif
  500. db->msg_enable = NETIF_MSG_LINK;
  501. db->mii.phy_id_mask = 0x1f;
  502. db->mii.reg_num_mask = 0x1f;
  503. db->mii.force_media = 0;
  504. db->mii.full_duplex = 0;
  505. db->mii.dev = ndev;
  506. db->mii.mdio_read = dm9000_phy_read;
  507. db->mii.mdio_write = dm9000_phy_write;
  508. /* try reading the node address from the attached EEPROM */
  509. for (i = 0; i < 6; i += 2)
  510. dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
  511. if (!is_valid_ether_addr(ndev->dev_addr)) {
  512. /* try reading from mac */
  513. for (i = 0; i < 6; i++)
  514. ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
  515. }
  516. if (!is_valid_ether_addr(ndev->dev_addr))
  517. dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
  518. "set using ifconfig\n", ndev->name);
  519. platform_set_drvdata(pdev, ndev);
  520. ret = register_netdev(ndev);
  521. if (ret == 0) {
  522. DECLARE_MAC_BUF(mac);
  523. printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n",
  524. ndev->name, db->io_addr, db->io_data, ndev->irq,
  525. print_mac(mac, ndev->dev_addr));
  526. }
  527. return 0;
  528. out:
  529. dev_err(db->dev, "not found (%d).\n", ret);
  530. dm9000_release_board(pdev, db);
  531. free_netdev(ndev);
  532. return ret;
  533. }
  534. /*
  535. * Open the interface.
  536. * The interface is opened whenever "ifconfig" actives it.
  537. */
  538. static int
  539. dm9000_open(struct net_device *dev)
  540. {
  541. board_info_t *db = (board_info_t *) dev->priv;
  542. unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
  543. dev_dbg(db->dev, "entering %s\n", __func__);
  544. /* If there is no IRQ type specified, default to something that
  545. * may work, and tell the user that this is a problem */
  546. if (irqflags == IRQF_TRIGGER_NONE) {
  547. dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
  548. irqflags = DEFAULT_TRIGGER;
  549. }
  550. irqflags |= IRQF_SHARED;
  551. if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
  552. return -EAGAIN;
  553. /* Initialize DM9000 board */
  554. dm9000_reset(db);
  555. dm9000_init_dm9000(dev);
  556. /* Init driver variable */
  557. db->dbug_cnt = 0;
  558. mii_check_media(&db->mii, netif_msg_link(db), 1);
  559. netif_start_queue(dev);
  560. return 0;
  561. }
  562. /*
  563. * Initilize dm9000 board
  564. */
  565. static void
  566. dm9000_init_dm9000(struct net_device *dev)
  567. {
  568. board_info_t *db = (board_info_t *) dev->priv;
  569. dm9000_dbg(db, 1, "entering %s\n", __func__);
  570. /* I/O mode */
  571. db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
  572. /* GPIO0 on pre-activate PHY */
  573. iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
  574. iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */
  575. iow(db, DM9000_GPR, 0); /* Enable PHY */
  576. if (db->flags & DM9000_PLATF_EXT_PHY)
  577. iow(db, DM9000_NCR, NCR_EXT_PHY);
  578. /* Program operating register */
  579. iow(db, DM9000_TCR, 0); /* TX Polling clear */
  580. iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  581. iow(db, DM9000_FCR, 0xff); /* Flow Control */
  582. iow(db, DM9000_SMCR, 0); /* Special Mode */
  583. /* clear TX status */
  584. iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  585. iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
  586. /* Set address filter table */
  587. dm9000_hash_table(dev);
  588. /* Activate DM9000 */
  589. iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  590. /* Enable TX/RX interrupt mask */
  591. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  592. /* Init Driver variable */
  593. db->tx_pkt_cnt = 0;
  594. db->queue_pkt_len = 0;
  595. dev->trans_start = 0;
  596. }
  597. /*
  598. * Hardware start transmission.
  599. * Send a packet to media from the upper layer.
  600. */
  601. static int
  602. dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  603. {
  604. unsigned long flags;
  605. board_info_t *db = (board_info_t *) dev->priv;
  606. dm9000_dbg(db, 3, "%s:\n", __func__);
  607. if (db->tx_pkt_cnt > 1)
  608. return 1;
  609. spin_lock_irqsave(&db->lock, flags);
  610. /* Move data to DM9000 TX RAM */
  611. writeb(DM9000_MWCMD, db->io_addr);
  612. (db->outblk)(db->io_data, skb->data, skb->len);
  613. dev->stats.tx_bytes += skb->len;
  614. db->tx_pkt_cnt++;
  615. /* TX control: First packet immediately send, second packet queue */
  616. if (db->tx_pkt_cnt == 1) {
  617. /* Set TX length to DM9000 */
  618. iow(db, DM9000_TXPLL, skb->len & 0xff);
  619. iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
  620. /* Issue TX polling command */
  621. iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  622. dev->trans_start = jiffies; /* save the time stamp */
  623. } else {
  624. /* Second packet */
  625. db->queue_pkt_len = skb->len;
  626. netif_stop_queue(dev);
  627. }
  628. spin_unlock_irqrestore(&db->lock, flags);
  629. /* free this SKB */
  630. dev_kfree_skb(skb);
  631. return 0;
  632. }
  633. static void
  634. dm9000_shutdown(struct net_device *dev)
  635. {
  636. board_info_t *db = (board_info_t *) dev->priv;
  637. /* RESET device */
  638. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  639. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  640. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  641. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  642. }
  643. /*
  644. * Stop the interface.
  645. * The interface is stopped when it is brought.
  646. */
  647. static int
  648. dm9000_stop(struct net_device *ndev)
  649. {
  650. board_info_t *db = (board_info_t *) ndev->priv;
  651. dm9000_dbg(db, 1, "entering %s\n", __func__);
  652. netif_stop_queue(ndev);
  653. netif_carrier_off(ndev);
  654. /* free interrupt */
  655. free_irq(ndev->irq, ndev);
  656. dm9000_shutdown(ndev);
  657. return 0;
  658. }
  659. /*
  660. * DM9000 interrupt handler
  661. * receive the packet to upper layer, free the transmitted packet
  662. */
  663. static void
  664. dm9000_tx_done(struct net_device *dev, board_info_t * db)
  665. {
  666. int tx_status = ior(db, DM9000_NSR); /* Got TX status */
  667. if (tx_status & (NSR_TX2END | NSR_TX1END)) {
  668. /* One packet sent complete */
  669. db->tx_pkt_cnt--;
  670. dev->stats.tx_packets++;
  671. /* Queue packet check & send */
  672. if (db->tx_pkt_cnt > 0) {
  673. iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
  674. iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
  675. iow(db, DM9000_TCR, TCR_TXREQ);
  676. dev->trans_start = jiffies;
  677. }
  678. netif_wake_queue(dev);
  679. }
  680. }
  681. static irqreturn_t
  682. dm9000_interrupt(int irq, void *dev_id)
  683. {
  684. struct net_device *dev = dev_id;
  685. board_info_t *db = (board_info_t *) dev->priv;
  686. int int_status;
  687. u8 reg_save;
  688. dm9000_dbg(db, 3, "entering %s\n", __func__);
  689. /* A real interrupt coming */
  690. spin_lock(&db->lock);
  691. /* Save previous register address */
  692. reg_save = readb(db->io_addr);
  693. /* Disable all interrupts */
  694. iow(db, DM9000_IMR, IMR_PAR);
  695. /* Got DM9000 interrupt status */
  696. int_status = ior(db, DM9000_ISR); /* Got ISR */
  697. iow(db, DM9000_ISR, int_status); /* Clear ISR status */
  698. /* Received the coming packet */
  699. if (int_status & ISR_PRS)
  700. dm9000_rx(dev);
  701. /* Trnasmit Interrupt check */
  702. if (int_status & ISR_PTS)
  703. dm9000_tx_done(dev, db);
  704. /* Re-enable interrupt mask */
  705. iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
  706. /* Restore previous register address */
  707. writeb(reg_save, db->io_addr);
  708. spin_unlock(&db->lock);
  709. return IRQ_HANDLED;
  710. }
  711. struct dm9000_rxhdr {
  712. u8 RxPktReady;
  713. u8 RxStatus;
  714. u16 RxLen;
  715. } __attribute__((__packed__));
  716. /*
  717. * Received a packet and pass to upper layer
  718. */
  719. static void
  720. dm9000_rx(struct net_device *dev)
  721. {
  722. board_info_t *db = (board_info_t *) dev->priv;
  723. struct dm9000_rxhdr rxhdr;
  724. struct sk_buff *skb;
  725. u8 rxbyte, *rdptr;
  726. bool GoodPacket;
  727. int RxLen;
  728. /* Check packet ready or not */
  729. do {
  730. ior(db, DM9000_MRCMDX); /* Dummy read */
  731. /* Get most updated data */
  732. rxbyte = readb(db->io_data);
  733. /* Status check: this byte must be 0 or 1 */
  734. if (rxbyte > DM9000_PKT_RDY) {
  735. dev_warn(db->dev, "status check fail: %d\n", rxbyte);
  736. iow(db, DM9000_RCR, 0x00); /* Stop Device */
  737. iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
  738. return;
  739. }
  740. if (rxbyte != DM9000_PKT_RDY)
  741. return;
  742. /* A packet ready now & Get status/length */
  743. GoodPacket = true;
  744. writeb(DM9000_MRCMD, db->io_addr);
  745. (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
  746. RxLen = le16_to_cpu(rxhdr.RxLen);
  747. /* Packet Status check */
  748. if (RxLen < 0x40) {
  749. GoodPacket = false;
  750. dev_dbg(db->dev, "Bad Packet received (runt)\n");
  751. }
  752. if (RxLen > DM9000_PKT_MAX) {
  753. dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
  754. }
  755. if (rxhdr.RxStatus & 0xbf) {
  756. GoodPacket = false;
  757. if (rxhdr.RxStatus & 0x01) {
  758. dev_dbg(db->dev, "fifo error\n");
  759. dev->stats.rx_fifo_errors++;
  760. }
  761. if (rxhdr.RxStatus & 0x02) {
  762. dev_dbg(db->dev, "crc error\n");
  763. dev->stats.rx_crc_errors++;
  764. }
  765. if (rxhdr.RxStatus & 0x80) {
  766. dev_dbg(db->dev, "length error\n");
  767. dev->stats.rx_length_errors++;
  768. }
  769. }
  770. /* Move data from DM9000 */
  771. if (GoodPacket
  772. && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
  773. skb_reserve(skb, 2);
  774. rdptr = (u8 *) skb_put(skb, RxLen - 4);
  775. /* Read received packet from RX SRAM */
  776. (db->inblk)(db->io_data, rdptr, RxLen);
  777. dev->stats.rx_bytes += RxLen;
  778. /* Pass to upper layer */
  779. skb->protocol = eth_type_trans(skb, dev);
  780. netif_rx(skb);
  781. dev->stats.rx_packets++;
  782. } else {
  783. /* need to dump the packet's data */
  784. (db->dumpblk)(db->io_data, RxLen);
  785. }
  786. } while (rxbyte == DM9000_PKT_RDY);
  787. }
  788. /*
  789. * Read a word data from EEPROM
  790. */
  791. static void
  792. dm9000_read_eeprom(board_info_t * db, int offset, unsigned char *to)
  793. {
  794. iow(db, DM9000_EPAR, offset);
  795. iow(db, DM9000_EPCR, EPCR_ERPRR);
  796. mdelay(8); /* according to the datasheet 200us should be enough,
  797. but it doesn't work */
  798. iow(db, DM9000_EPCR, 0x0);
  799. to[0] = ior(db, DM9000_EPDRL);
  800. to[1] = ior(db, DM9000_EPDRH);
  801. }
  802. #ifdef DM9000_PROGRAM_EEPROM
  803. /*
  804. * Write a word data to SROM
  805. */
  806. static void
  807. write_srom_word(board_info_t * db, int offset, u16 val)
  808. {
  809. iow(db, DM9000_EPAR, offset);
  810. iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
  811. iow(db, DM9000_EPDRL, (val & 0xff));
  812. iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
  813. mdelay(8); /* same shit */
  814. iow(db, DM9000_EPCR, 0);
  815. }
  816. /*
  817. * Only for development:
  818. * Here we write static data to the eeprom in case
  819. * we don't have valid content on a new board
  820. */
  821. static void
  822. program_eeprom(board_info_t * db)
  823. {
  824. u16 eeprom[] = { 0x0c00, 0x007f, 0x1300, /* MAC Address */
  825. 0x0000, /* Autoload: accept nothing */
  826. 0x0a46, 0x9000, /* Vendor / Product ID */
  827. 0x0000, /* pin control */
  828. 0x0000,
  829. }; /* Wake-up mode control */
  830. int i;
  831. for (i = 0; i < 8; i++)
  832. write_srom_word(db, i, eeprom[i]);
  833. }
  834. #endif
  835. /*
  836. * Calculate the CRC valude of the Rx packet
  837. * flag = 1 : return the reverse CRC (for the received packet CRC)
  838. * 0 : return the normal CRC (for Hash Table index)
  839. */
  840. static unsigned long
  841. cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
  842. {
  843. u32 crc = ether_crc_le(Len, Data);
  844. if (flag)
  845. return ~crc;
  846. return crc;
  847. }
  848. /*
  849. * Set DM9000 multicast address
  850. */
  851. static void
  852. dm9000_hash_table(struct net_device *dev)
  853. {
  854. board_info_t *db = (board_info_t *) dev->priv;
  855. struct dev_mc_list *mcptr = dev->mc_list;
  856. int mc_cnt = dev->mc_count;
  857. u32 hash_val;
  858. u16 i, oft, hash_table[4];
  859. unsigned long flags;
  860. dm9000_dbg(db, 1, "entering %s\n", __func__);
  861. spin_lock_irqsave(&db->lock,flags);
  862. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  863. iow(db, oft, dev->dev_addr[i]);
  864. /* Clear Hash Table */
  865. for (i = 0; i < 4; i++)
  866. hash_table[i] = 0x0;
  867. /* broadcast address */
  868. hash_table[3] = 0x8000;
  869. /* the multicast address in Hash Table : 64 bits */
  870. for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
  871. hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
  872. hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
  873. }
  874. /* Write the hash table to MAC MD table */
  875. for (i = 0, oft = 0x16; i < 4; i++) {
  876. iow(db, oft++, hash_table[i] & 0xff);
  877. iow(db, oft++, (hash_table[i] >> 8) & 0xff);
  878. }
  879. spin_unlock_irqrestore(&db->lock,flags);
  880. }
  881. /*
  882. * Sleep, either by using msleep() or if we are suspending, then
  883. * use mdelay() to sleep.
  884. */
  885. static void dm9000_msleep(board_info_t *db, unsigned int ms)
  886. {
  887. if (db->in_suspend)
  888. mdelay(ms);
  889. else
  890. msleep(ms);
  891. }
  892. /*
  893. * Read a word from phyxcer
  894. */
  895. static int
  896. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  897. {
  898. board_info_t *db = (board_info_t *) dev->priv;
  899. unsigned long flags;
  900. unsigned int reg_save;
  901. int ret;
  902. spin_lock_irqsave(&db->lock,flags);
  903. /* Save previous register address */
  904. reg_save = readb(db->io_addr);
  905. /* Fill the phyxcer register into REG_0C */
  906. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  907. iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  908. writeb(reg_save, db->io_addr);
  909. spin_unlock_irqrestore(&db->lock,flags);
  910. dm9000_msleep(db, 1); /* Wait read complete */
  911. spin_lock_irqsave(&db->lock,flags);
  912. reg_save = readb(db->io_addr);
  913. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  914. /* The read data keeps on REG_0D & REG_0E */
  915. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
  916. /* restore the previous address */
  917. writeb(reg_save, db->io_addr);
  918. spin_unlock_irqrestore(&db->lock,flags);
  919. return ret;
  920. }
  921. /*
  922. * Write a word to phyxcer
  923. */
  924. static void
  925. dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
  926. {
  927. board_info_t *db = (board_info_t *) dev->priv;
  928. unsigned long flags;
  929. unsigned long reg_save;
  930. spin_lock_irqsave(&db->lock,flags);
  931. /* Save previous register address */
  932. reg_save = readb(db->io_addr);
  933. /* Fill the phyxcer register into REG_0C */
  934. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  935. /* Fill the written data into REG_0D & REG_0E */
  936. iow(db, DM9000_EPDRL, (value & 0xff));
  937. iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
  938. iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  939. writeb(reg_save, db->io_addr);
  940. spin_unlock_irqrestore(&db->lock,flags);
  941. dm9000_msleep(db, 1); /* Wait write complete */
  942. spin_lock_irqsave(&db->lock,flags);
  943. reg_save = readb(db->io_addr);
  944. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  945. /* restore the previous address */
  946. writeb(reg_save, db->io_addr);
  947. spin_unlock_irqrestore(&db->lock,flags);
  948. }
  949. static int
  950. dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
  951. {
  952. struct net_device *ndev = platform_get_drvdata(dev);
  953. board_info_t *db;
  954. if (ndev) {
  955. db = (board_info_t *) ndev->priv;
  956. db->in_suspend = 1;
  957. if (netif_running(ndev)) {
  958. netif_device_detach(ndev);
  959. dm9000_shutdown(ndev);
  960. }
  961. }
  962. return 0;
  963. }
  964. static int
  965. dm9000_drv_resume(struct platform_device *dev)
  966. {
  967. struct net_device *ndev = platform_get_drvdata(dev);
  968. board_info_t *db = (board_info_t *) ndev->priv;
  969. if (ndev) {
  970. if (netif_running(ndev)) {
  971. dm9000_reset(db);
  972. dm9000_init_dm9000(ndev);
  973. netif_device_attach(ndev);
  974. }
  975. db->in_suspend = 0;
  976. }
  977. return 0;
  978. }
  979. static int
  980. dm9000_drv_remove(struct platform_device *pdev)
  981. {
  982. struct net_device *ndev = platform_get_drvdata(pdev);
  983. platform_set_drvdata(pdev, NULL);
  984. unregister_netdev(ndev);
  985. dm9000_release_board(pdev, (board_info_t *) ndev->priv);
  986. free_netdev(ndev); /* free device structure */
  987. dev_dbg(&pdev->dev, "released and freed device\n");
  988. return 0;
  989. }
  990. static struct platform_driver dm9000_driver = {
  991. .driver = {
  992. .name = "dm9000",
  993. .owner = THIS_MODULE,
  994. },
  995. .probe = dm9000_probe,
  996. .remove = dm9000_drv_remove,
  997. .suspend = dm9000_drv_suspend,
  998. .resume = dm9000_drv_resume,
  999. };
  1000. static int __init
  1001. dm9000_init(void)
  1002. {
  1003. printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
  1004. return platform_driver_register(&dm9000_driver); /* search board and register */
  1005. }
  1006. static void __exit
  1007. dm9000_cleanup(void)
  1008. {
  1009. platform_driver_unregister(&dm9000_driver);
  1010. }
  1011. module_init(dm9000_init);
  1012. module_exit(dm9000_cleanup);
  1013. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  1014. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  1015. MODULE_LICENSE("GPL");