dm9000.c 42 KB

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