dm9000.c 40 KB

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