dm9000.c 40 KB

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