dm9000.c 32 KB

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