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