dm9000.c 41 KB

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