dm9000.c 33 KB

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