dm9000.c 42 KB

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