dm9000.c 33 KB

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