dm9000.c 33 KB

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