dm9000.c 39 KB

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