dm9000.c 33 KB

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