dm9000.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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 = container_of(w, struct delayed_work, work);
  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. u8 reg_save;
  733. dm9000_dbg(db, 3, "entering %s\n", __func__);
  734. /* A real interrupt coming */
  735. spin_lock(&db->lock);
  736. /* Save previous register address */
  737. reg_save = readb(db->io_addr);
  738. /* Disable all interrupts */
  739. iow(db, DM9000_IMR, IMR_PAR);
  740. /* Got DM9000 interrupt status */
  741. int_status = ior(db, DM9000_ISR); /* Got ISR */
  742. iow(db, DM9000_ISR, int_status); /* Clear ISR status */
  743. if (netif_msg_intr(db))
  744. dev_dbg(db->dev, "interrupt status %02x\n", int_status);
  745. /* Received the coming packet */
  746. if (int_status & ISR_PRS)
  747. dm9000_rx(dev);
  748. /* Trnasmit Interrupt check */
  749. if (int_status & ISR_PTS)
  750. dm9000_tx_done(dev, db);
  751. if (db->type != TYPE_DM9000E) {
  752. if (int_status & ISR_LNKCHNG) {
  753. /* fire a link-change request */
  754. schedule_delayed_work(&db->phy_poll, 1);
  755. }
  756. }
  757. /* Re-enable interrupt mask */
  758. iow(db, DM9000_IMR, db->imr_all);
  759. /* Restore previous register address */
  760. writeb(reg_save, db->io_addr);
  761. spin_unlock(&db->lock);
  762. return IRQ_HANDLED;
  763. }
  764. #ifdef CONFIG_NET_POLL_CONTROLLER
  765. /*
  766. *Used by netconsole
  767. */
  768. static void dm9000_poll_controller(struct net_device *dev)
  769. {
  770. disable_irq(dev->irq);
  771. dm9000_interrupt(dev->irq, dev);
  772. enable_irq(dev->irq);
  773. }
  774. #endif
  775. /*
  776. * Open the interface.
  777. * The interface is opened whenever "ifconfig" actives it.
  778. */
  779. static int
  780. dm9000_open(struct net_device *dev)
  781. {
  782. board_info_t *db = netdev_priv(dev);
  783. unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
  784. if (netif_msg_ifup(db))
  785. dev_dbg(db->dev, "enabling %s\n", dev->name);
  786. /* If there is no IRQ type specified, default to something that
  787. * may work, and tell the user that this is a problem */
  788. if (irqflags == IRQF_TRIGGER_NONE)
  789. dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
  790. irqflags |= IRQF_SHARED;
  791. if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
  792. return -EAGAIN;
  793. /* Initialize DM9000 board */
  794. dm9000_reset(db);
  795. dm9000_init_dm9000(dev);
  796. /* Init driver variable */
  797. db->dbug_cnt = 0;
  798. mii_check_media(&db->mii, netif_msg_link(db), 1);
  799. netif_start_queue(dev);
  800. dm9000_schedule_poll(db);
  801. return 0;
  802. }
  803. /*
  804. * Sleep, either by using msleep() or if we are suspending, then
  805. * use mdelay() to sleep.
  806. */
  807. static void dm9000_msleep(board_info_t *db, unsigned int ms)
  808. {
  809. if (db->in_suspend)
  810. mdelay(ms);
  811. else
  812. msleep(ms);
  813. }
  814. /*
  815. * Read a word from phyxcer
  816. */
  817. static int
  818. dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
  819. {
  820. board_info_t *db = netdev_priv(dev);
  821. unsigned long flags;
  822. unsigned int reg_save;
  823. int ret;
  824. mutex_lock(&db->addr_lock);
  825. spin_lock_irqsave(&db->lock,flags);
  826. /* Save previous register address */
  827. reg_save = readb(db->io_addr);
  828. /* Fill the phyxcer register into REG_0C */
  829. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  830. iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS); /* Issue phyxcer read command */
  831. writeb(reg_save, db->io_addr);
  832. spin_unlock_irqrestore(&db->lock,flags);
  833. dm9000_msleep(db, 1); /* Wait read complete */
  834. spin_lock_irqsave(&db->lock,flags);
  835. reg_save = readb(db->io_addr);
  836. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  837. /* The read data keeps on REG_0D & REG_0E */
  838. ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
  839. /* restore the previous address */
  840. writeb(reg_save, db->io_addr);
  841. spin_unlock_irqrestore(&db->lock,flags);
  842. mutex_unlock(&db->addr_lock);
  843. dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
  844. return ret;
  845. }
  846. /*
  847. * Write a word to phyxcer
  848. */
  849. static void
  850. dm9000_phy_write(struct net_device *dev,
  851. int phyaddr_unused, int reg, int value)
  852. {
  853. board_info_t *db = netdev_priv(dev);
  854. unsigned long flags;
  855. unsigned long reg_save;
  856. dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
  857. mutex_lock(&db->addr_lock);
  858. spin_lock_irqsave(&db->lock,flags);
  859. /* Save previous register address */
  860. reg_save = readb(db->io_addr);
  861. /* Fill the phyxcer register into REG_0C */
  862. iow(db, DM9000_EPAR, DM9000_PHY | reg);
  863. /* Fill the written data into REG_0D & REG_0E */
  864. iow(db, DM9000_EPDRL, value);
  865. iow(db, DM9000_EPDRH, value >> 8);
  866. iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW); /* Issue phyxcer write command */
  867. writeb(reg_save, db->io_addr);
  868. spin_unlock_irqrestore(&db->lock, flags);
  869. dm9000_msleep(db, 1); /* Wait write complete */
  870. spin_lock_irqsave(&db->lock,flags);
  871. reg_save = readb(db->io_addr);
  872. iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  873. /* restore the previous address */
  874. writeb(reg_save, db->io_addr);
  875. spin_unlock_irqrestore(&db->lock, flags);
  876. mutex_unlock(&db->addr_lock);
  877. }
  878. static void
  879. dm9000_shutdown(struct net_device *dev)
  880. {
  881. board_info_t *db = netdev_priv(dev);
  882. /* RESET device */
  883. dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
  884. iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  885. iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */
  886. iow(db, DM9000_RCR, 0x00); /* Disable RX */
  887. }
  888. /*
  889. * Stop the interface.
  890. * The interface is stopped when it is brought.
  891. */
  892. static int
  893. dm9000_stop(struct net_device *ndev)
  894. {
  895. board_info_t *db = netdev_priv(ndev);
  896. if (netif_msg_ifdown(db))
  897. dev_dbg(db->dev, "shutting down %s\n", ndev->name);
  898. cancel_delayed_work_sync(&db->phy_poll);
  899. netif_stop_queue(ndev);
  900. netif_carrier_off(ndev);
  901. /* free interrupt */
  902. free_irq(ndev->irq, ndev);
  903. dm9000_shutdown(ndev);
  904. return 0;
  905. }
  906. #define res_size(_r) (((_r)->end - (_r)->start) + 1)
  907. /*
  908. * Search DM9000 board, allocate space and register it
  909. */
  910. static int __devinit
  911. dm9000_probe(struct platform_device *pdev)
  912. {
  913. struct dm9000_plat_data *pdata = pdev->dev.platform_data;
  914. struct board_info *db; /* Point a board information structure */
  915. struct net_device *ndev;
  916. const unsigned char *mac_src;
  917. int ret = 0;
  918. int iosize;
  919. int i;
  920. u32 id_val;
  921. /* Init network device */
  922. ndev = alloc_etherdev(sizeof(struct board_info));
  923. if (!ndev) {
  924. dev_err(&pdev->dev, "could not allocate device.\n");
  925. return -ENOMEM;
  926. }
  927. SET_NETDEV_DEV(ndev, &pdev->dev);
  928. dev_dbg(&pdev->dev, "dm9000_probe()\n");
  929. /* setup board info structure */
  930. db = netdev_priv(ndev);
  931. memset(db, 0, sizeof(*db));
  932. db->dev = &pdev->dev;
  933. db->ndev = ndev;
  934. spin_lock_init(&db->lock);
  935. mutex_init(&db->addr_lock);
  936. INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
  937. db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  938. db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  939. db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  940. if (db->addr_res == NULL || db->data_res == NULL ||
  941. db->irq_res == NULL) {
  942. dev_err(db->dev, "insufficient resources\n");
  943. ret = -ENOENT;
  944. goto out;
  945. }
  946. iosize = res_size(db->addr_res);
  947. db->addr_req = request_mem_region(db->addr_res->start, iosize,
  948. pdev->name);
  949. if (db->addr_req == NULL) {
  950. dev_err(db->dev, "cannot claim address reg area\n");
  951. ret = -EIO;
  952. goto out;
  953. }
  954. db->io_addr = ioremap(db->addr_res->start, iosize);
  955. if (db->io_addr == NULL) {
  956. dev_err(db->dev, "failed to ioremap address reg\n");
  957. ret = -EINVAL;
  958. goto out;
  959. }
  960. iosize = res_size(db->data_res);
  961. db->data_req = request_mem_region(db->data_res->start, iosize,
  962. pdev->name);
  963. if (db->data_req == NULL) {
  964. dev_err(db->dev, "cannot claim data reg area\n");
  965. ret = -EIO;
  966. goto out;
  967. }
  968. db->io_data = ioremap(db->data_res->start, iosize);
  969. if (db->io_data == NULL) {
  970. dev_err(db->dev, "failed to ioremap data reg\n");
  971. ret = -EINVAL;
  972. goto out;
  973. }
  974. /* fill in parameters for net-dev structure */
  975. ndev->base_addr = (unsigned long)db->io_addr;
  976. ndev->irq = db->irq_res->start;
  977. /* ensure at least we have a default set of IO routines */
  978. dm9000_set_io(db, iosize);
  979. /* check to see if anything is being over-ridden */
  980. if (pdata != NULL) {
  981. /* check to see if the driver wants to over-ride the
  982. * default IO width */
  983. if (pdata->flags & DM9000_PLATF_8BITONLY)
  984. dm9000_set_io(db, 1);
  985. if (pdata->flags & DM9000_PLATF_16BITONLY)
  986. dm9000_set_io(db, 2);
  987. if (pdata->flags & DM9000_PLATF_32BITONLY)
  988. dm9000_set_io(db, 4);
  989. /* check to see if there are any IO routine
  990. * over-rides */
  991. if (pdata->inblk != NULL)
  992. db->inblk = pdata->inblk;
  993. if (pdata->outblk != NULL)
  994. db->outblk = pdata->outblk;
  995. if (pdata->dumpblk != NULL)
  996. db->dumpblk = pdata->dumpblk;
  997. db->flags = pdata->flags;
  998. }
  999. #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
  1000. db->flags |= DM9000_PLATF_SIMPLE_PHY;
  1001. #endif
  1002. dm9000_reset(db);
  1003. /* try multiple times, DM9000 sometimes gets the read wrong */
  1004. for (i = 0; i < 8; i++) {
  1005. id_val = ior(db, DM9000_VIDL);
  1006. id_val |= (u32)ior(db, DM9000_VIDH) << 8;
  1007. id_val |= (u32)ior(db, DM9000_PIDL) << 16;
  1008. id_val |= (u32)ior(db, DM9000_PIDH) << 24;
  1009. if (id_val == DM9000_ID)
  1010. break;
  1011. dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
  1012. }
  1013. if (id_val != DM9000_ID) {
  1014. dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
  1015. ret = -ENODEV;
  1016. goto out;
  1017. }
  1018. /* Identify what type of DM9000 we are working on */
  1019. id_val = ior(db, DM9000_CHIPR);
  1020. dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
  1021. switch (id_val) {
  1022. case CHIPR_DM9000A:
  1023. db->type = TYPE_DM9000A;
  1024. break;
  1025. case CHIPR_DM9000B:
  1026. db->type = TYPE_DM9000B;
  1027. break;
  1028. default:
  1029. dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
  1030. db->type = TYPE_DM9000E;
  1031. }
  1032. /* from this point we assume that we have found a DM9000 */
  1033. /* driver system function */
  1034. ether_setup(ndev);
  1035. ndev->open = &dm9000_open;
  1036. ndev->hard_start_xmit = &dm9000_start_xmit;
  1037. ndev->tx_timeout = &dm9000_timeout;
  1038. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1039. ndev->stop = &dm9000_stop;
  1040. ndev->set_multicast_list = &dm9000_hash_table;
  1041. ndev->ethtool_ops = &dm9000_ethtool_ops;
  1042. ndev->do_ioctl = &dm9000_ioctl;
  1043. #ifdef CONFIG_NET_POLL_CONTROLLER
  1044. ndev->poll_controller = &dm9000_poll_controller;
  1045. #endif
  1046. db->msg_enable = NETIF_MSG_LINK;
  1047. db->mii.phy_id_mask = 0x1f;
  1048. db->mii.reg_num_mask = 0x1f;
  1049. db->mii.force_media = 0;
  1050. db->mii.full_duplex = 0;
  1051. db->mii.dev = ndev;
  1052. db->mii.mdio_read = dm9000_phy_read;
  1053. db->mii.mdio_write = dm9000_phy_write;
  1054. mac_src = "eeprom";
  1055. /* try reading the node address from the attached EEPROM */
  1056. for (i = 0; i < 6; i += 2)
  1057. dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
  1058. if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
  1059. mac_src = "platform data";
  1060. memcpy(ndev->dev_addr, pdata->dev_addr, 6);
  1061. }
  1062. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1063. /* try reading from mac */
  1064. mac_src = "chip";
  1065. for (i = 0; i < 6; i++)
  1066. ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
  1067. }
  1068. if (!is_valid_ether_addr(ndev->dev_addr))
  1069. dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
  1070. "set using ifconfig\n", ndev->name);
  1071. platform_set_drvdata(pdev, ndev);
  1072. ret = register_netdev(ndev);
  1073. if (ret == 0)
  1074. printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
  1075. ndev->name, dm9000_type_to_char(db->type),
  1076. db->io_addr, db->io_data, ndev->irq,
  1077. ndev->dev_addr, mac_src);
  1078. return 0;
  1079. out:
  1080. dev_err(db->dev, "not found (%d).\n", ret);
  1081. dm9000_release_board(pdev, db);
  1082. free_netdev(ndev);
  1083. return ret;
  1084. }
  1085. static int
  1086. dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
  1087. {
  1088. struct net_device *ndev = platform_get_drvdata(dev);
  1089. board_info_t *db;
  1090. if (ndev) {
  1091. db = netdev_priv(ndev);
  1092. db->in_suspend = 1;
  1093. if (netif_running(ndev)) {
  1094. netif_device_detach(ndev);
  1095. dm9000_shutdown(ndev);
  1096. }
  1097. }
  1098. return 0;
  1099. }
  1100. static int
  1101. dm9000_drv_resume(struct platform_device *dev)
  1102. {
  1103. struct net_device *ndev = platform_get_drvdata(dev);
  1104. board_info_t *db = netdev_priv(ndev);
  1105. if (ndev) {
  1106. if (netif_running(ndev)) {
  1107. dm9000_reset(db);
  1108. dm9000_init_dm9000(ndev);
  1109. netif_device_attach(ndev);
  1110. }
  1111. db->in_suspend = 0;
  1112. }
  1113. return 0;
  1114. }
  1115. static int __devexit
  1116. dm9000_drv_remove(struct platform_device *pdev)
  1117. {
  1118. struct net_device *ndev = platform_get_drvdata(pdev);
  1119. platform_set_drvdata(pdev, NULL);
  1120. unregister_netdev(ndev);
  1121. dm9000_release_board(pdev, (board_info_t *) netdev_priv(ndev));
  1122. free_netdev(ndev); /* free device structure */
  1123. dev_dbg(&pdev->dev, "released and freed device\n");
  1124. return 0;
  1125. }
  1126. static struct platform_driver dm9000_driver = {
  1127. .driver = {
  1128. .name = "dm9000",
  1129. .owner = THIS_MODULE,
  1130. },
  1131. .probe = dm9000_probe,
  1132. .remove = __devexit_p(dm9000_drv_remove),
  1133. .suspend = dm9000_drv_suspend,
  1134. .resume = dm9000_drv_resume,
  1135. };
  1136. static int __init
  1137. dm9000_init(void)
  1138. {
  1139. printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
  1140. return platform_driver_register(&dm9000_driver);
  1141. }
  1142. static void __exit
  1143. dm9000_cleanup(void)
  1144. {
  1145. platform_driver_unregister(&dm9000_driver);
  1146. }
  1147. module_init(dm9000_init);
  1148. module_exit(dm9000_cleanup);
  1149. MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
  1150. MODULE_DESCRIPTION("Davicom DM9000 network driver");
  1151. MODULE_LICENSE("GPL");
  1152. MODULE_ALIAS("platform:dm9000");