dm9000.c 39 KB

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