tsi108_eth.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /*******************************************************************************
  2. Copyright(c) 2006 Tundra Semiconductor Corporation.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 2 of the License, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc., 59
  13. Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. *******************************************************************************/
  15. /* This driver is based on the driver code originally developed
  16. * for the Intel IOC80314 (ForestLake) Gigabit Ethernet by
  17. * scott.wood@timesys.com * Copyright (C) 2003 TimeSys Corporation
  18. *
  19. * Currently changes from original version are:
  20. * - porting to Tsi108-based platform and kernel 2.6 (kong.lai@tundra.com)
  21. * - modifications to handle two ports independently and support for
  22. * additional PHY devices (alexandre.bounine@tundra.com)
  23. * - Get hardware information from platform device. (tie-fei.zang@freescale.com)
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/init.h>
  29. #include <linux/net.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/delay.h>
  36. #include <linux/crc32.h>
  37. #include <linux/mii.h>
  38. #include <linux/device.h>
  39. #include <linux/pci.h>
  40. #include <linux/rtnetlink.h>
  41. #include <linux/timer.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/gfp.h>
  44. #include <asm/system.h>
  45. #include <asm/io.h>
  46. #include <asm/tsi108.h>
  47. #include "tsi108_eth.h"
  48. #define MII_READ_DELAY 10000 /* max link wait time in msec */
  49. #define TSI108_RXRING_LEN 256
  50. /* NOTE: The driver currently does not support receiving packets
  51. * larger than the buffer size, so don't decrease this (unless you
  52. * want to add such support).
  53. */
  54. #define TSI108_RXBUF_SIZE 1536
  55. #define TSI108_TXRING_LEN 256
  56. #define TSI108_TX_INT_FREQ 64
  57. /* Check the phy status every half a second. */
  58. #define CHECK_PHY_INTERVAL (HZ/2)
  59. static int tsi108_init_one(struct platform_device *pdev);
  60. static int tsi108_ether_remove(struct platform_device *pdev);
  61. struct tsi108_prv_data {
  62. void __iomem *regs; /* Base of normal regs */
  63. void __iomem *phyregs; /* Base of register bank used for PHY access */
  64. struct net_device *dev;
  65. struct napi_struct napi;
  66. unsigned int phy; /* Index of PHY for this interface */
  67. unsigned int irq_num;
  68. unsigned int id;
  69. unsigned int phy_type;
  70. struct timer_list timer;/* Timer that triggers the check phy function */
  71. unsigned int rxtail; /* Next entry in rxring to read */
  72. unsigned int rxhead; /* Next entry in rxring to give a new buffer */
  73. unsigned int rxfree; /* Number of free, allocated RX buffers */
  74. unsigned int rxpending; /* Non-zero if there are still descriptors
  75. * to be processed from a previous descriptor
  76. * interrupt condition that has been cleared */
  77. unsigned int txtail; /* Next TX descriptor to check status on */
  78. unsigned int txhead; /* Next TX descriptor to use */
  79. /* Number of free TX descriptors. This could be calculated from
  80. * rxhead and rxtail if one descriptor were left unused to disambiguate
  81. * full and empty conditions, but it's simpler to just keep track
  82. * explicitly. */
  83. unsigned int txfree;
  84. unsigned int phy_ok; /* The PHY is currently powered on. */
  85. /* PHY status (duplex is 1 for half, 2 for full,
  86. * so that the default 0 indicates that neither has
  87. * yet been configured). */
  88. unsigned int link_up;
  89. unsigned int speed;
  90. unsigned int duplex;
  91. tx_desc *txring;
  92. rx_desc *rxring;
  93. struct sk_buff *txskbs[TSI108_TXRING_LEN];
  94. struct sk_buff *rxskbs[TSI108_RXRING_LEN];
  95. dma_addr_t txdma, rxdma;
  96. /* txlock nests in misclock and phy_lock */
  97. spinlock_t txlock, misclock;
  98. /* stats is used to hold the upper bits of each hardware counter,
  99. * and tmpstats is used to hold the full values for returning
  100. * to the caller of get_stats(). They must be separate in case
  101. * an overflow interrupt occurs before the stats are consumed.
  102. */
  103. struct net_device_stats stats;
  104. struct net_device_stats tmpstats;
  105. /* These stats are kept separate in hardware, thus require individual
  106. * fields for handling carry. They are combined in get_stats.
  107. */
  108. unsigned long rx_fcs; /* Add to rx_frame_errors */
  109. unsigned long rx_short_fcs; /* Add to rx_frame_errors */
  110. unsigned long rx_long_fcs; /* Add to rx_frame_errors */
  111. unsigned long rx_underruns; /* Add to rx_length_errors */
  112. unsigned long rx_overruns; /* Add to rx_length_errors */
  113. unsigned long tx_coll_abort; /* Add to tx_aborted_errors/collisions */
  114. unsigned long tx_pause_drop; /* Add to tx_aborted_errors */
  115. unsigned long mc_hash[16];
  116. u32 msg_enable; /* debug message level */
  117. struct mii_if_info mii_if;
  118. unsigned int init_media;
  119. };
  120. /* Structure for a device driver */
  121. static struct platform_driver tsi_eth_driver = {
  122. .probe = tsi108_init_one,
  123. .remove = tsi108_ether_remove,
  124. .driver = {
  125. .name = "tsi-ethernet",
  126. .owner = THIS_MODULE,
  127. },
  128. };
  129. static void tsi108_timed_checker(unsigned long dev_ptr);
  130. static void dump_eth_one(struct net_device *dev)
  131. {
  132. struct tsi108_prv_data *data = netdev_priv(dev);
  133. printk("Dumping %s...\n", dev->name);
  134. printk("intstat %x intmask %x phy_ok %d"
  135. " link %d speed %d duplex %d\n",
  136. TSI_READ(TSI108_EC_INTSTAT),
  137. TSI_READ(TSI108_EC_INTMASK), data->phy_ok,
  138. data->link_up, data->speed, data->duplex);
  139. printk("TX: head %d, tail %d, free %d, stat %x, estat %x, err %x\n",
  140. data->txhead, data->txtail, data->txfree,
  141. TSI_READ(TSI108_EC_TXSTAT),
  142. TSI_READ(TSI108_EC_TXESTAT),
  143. TSI_READ(TSI108_EC_TXERR));
  144. printk("RX: head %d, tail %d, free %d, stat %x,"
  145. " estat %x, err %x, pending %d\n\n",
  146. data->rxhead, data->rxtail, data->rxfree,
  147. TSI_READ(TSI108_EC_RXSTAT),
  148. TSI_READ(TSI108_EC_RXESTAT),
  149. TSI_READ(TSI108_EC_RXERR), data->rxpending);
  150. }
  151. /* Synchronization is needed between the thread and up/down events.
  152. * Note that the PHY is accessed through the same registers for both
  153. * interfaces, so this can't be made interface-specific.
  154. */
  155. static DEFINE_SPINLOCK(phy_lock);
  156. static int tsi108_read_mii(struct tsi108_prv_data *data, int reg)
  157. {
  158. unsigned i;
  159. TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
  160. (data->phy << TSI108_MAC_MII_ADDR_PHY) |
  161. (reg << TSI108_MAC_MII_ADDR_REG));
  162. TSI_WRITE_PHY(TSI108_MAC_MII_CMD, 0);
  163. TSI_WRITE_PHY(TSI108_MAC_MII_CMD, TSI108_MAC_MII_CMD_READ);
  164. for (i = 0; i < 100; i++) {
  165. if (!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
  166. (TSI108_MAC_MII_IND_NOTVALID | TSI108_MAC_MII_IND_BUSY)))
  167. break;
  168. udelay(10);
  169. }
  170. if (i == 100)
  171. return 0xffff;
  172. else
  173. return TSI_READ_PHY(TSI108_MAC_MII_DATAIN);
  174. }
  175. static void tsi108_write_mii(struct tsi108_prv_data *data,
  176. int reg, u16 val)
  177. {
  178. unsigned i = 100;
  179. TSI_WRITE_PHY(TSI108_MAC_MII_ADDR,
  180. (data->phy << TSI108_MAC_MII_ADDR_PHY) |
  181. (reg << TSI108_MAC_MII_ADDR_REG));
  182. TSI_WRITE_PHY(TSI108_MAC_MII_DATAOUT, val);
  183. while (i--) {
  184. if(!(TSI_READ_PHY(TSI108_MAC_MII_IND) &
  185. TSI108_MAC_MII_IND_BUSY))
  186. break;
  187. udelay(10);
  188. }
  189. }
  190. static int tsi108_mdio_read(struct net_device *dev, int addr, int reg)
  191. {
  192. struct tsi108_prv_data *data = netdev_priv(dev);
  193. return tsi108_read_mii(data, reg);
  194. }
  195. static void tsi108_mdio_write(struct net_device *dev, int addr, int reg, int val)
  196. {
  197. struct tsi108_prv_data *data = netdev_priv(dev);
  198. tsi108_write_mii(data, reg, val);
  199. }
  200. static inline void tsi108_write_tbi(struct tsi108_prv_data *data,
  201. int reg, u16 val)
  202. {
  203. unsigned i = 1000;
  204. TSI_WRITE(TSI108_MAC_MII_ADDR,
  205. (0x1e << TSI108_MAC_MII_ADDR_PHY)
  206. | (reg << TSI108_MAC_MII_ADDR_REG));
  207. TSI_WRITE(TSI108_MAC_MII_DATAOUT, val);
  208. while(i--) {
  209. if(!(TSI_READ(TSI108_MAC_MII_IND) & TSI108_MAC_MII_IND_BUSY))
  210. return;
  211. udelay(10);
  212. }
  213. printk(KERN_ERR "%s function time out\n", __func__);
  214. }
  215. static int mii_speed(struct mii_if_info *mii)
  216. {
  217. int advert, lpa, val, media;
  218. int lpa2 = 0;
  219. int speed;
  220. if (!mii_link_ok(mii))
  221. return 0;
  222. val = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_BMSR);
  223. if ((val & BMSR_ANEGCOMPLETE) == 0)
  224. return 0;
  225. advert = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_ADVERTISE);
  226. lpa = (*mii->mdio_read) (mii->dev, mii->phy_id, MII_LPA);
  227. media = mii_nway_result(advert & lpa);
  228. if (mii->supports_gmii)
  229. lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
  230. speed = lpa2 & (LPA_1000FULL | LPA_1000HALF) ? 1000 :
  231. (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 100 : 10);
  232. return speed;
  233. }
  234. static void tsi108_check_phy(struct net_device *dev)
  235. {
  236. struct tsi108_prv_data *data = netdev_priv(dev);
  237. u32 mac_cfg2_reg, portctrl_reg;
  238. u32 duplex;
  239. u32 speed;
  240. unsigned long flags;
  241. spin_lock_irqsave(&phy_lock, flags);
  242. if (!data->phy_ok)
  243. goto out;
  244. duplex = mii_check_media(&data->mii_if, netif_msg_link(data), data->init_media);
  245. data->init_media = 0;
  246. if (netif_carrier_ok(dev)) {
  247. speed = mii_speed(&data->mii_if);
  248. if ((speed != data->speed) || duplex) {
  249. mac_cfg2_reg = TSI_READ(TSI108_MAC_CFG2);
  250. portctrl_reg = TSI_READ(TSI108_EC_PORTCTRL);
  251. mac_cfg2_reg &= ~TSI108_MAC_CFG2_IFACE_MASK;
  252. if (speed == 1000) {
  253. mac_cfg2_reg |= TSI108_MAC_CFG2_GIG;
  254. portctrl_reg &= ~TSI108_EC_PORTCTRL_NOGIG;
  255. } else {
  256. mac_cfg2_reg |= TSI108_MAC_CFG2_NOGIG;
  257. portctrl_reg |= TSI108_EC_PORTCTRL_NOGIG;
  258. }
  259. data->speed = speed;
  260. if (data->mii_if.full_duplex) {
  261. mac_cfg2_reg |= TSI108_MAC_CFG2_FULLDUPLEX;
  262. portctrl_reg &= ~TSI108_EC_PORTCTRL_HALFDUPLEX;
  263. data->duplex = 2;
  264. } else {
  265. mac_cfg2_reg &= ~TSI108_MAC_CFG2_FULLDUPLEX;
  266. portctrl_reg |= TSI108_EC_PORTCTRL_HALFDUPLEX;
  267. data->duplex = 1;
  268. }
  269. TSI_WRITE(TSI108_MAC_CFG2, mac_cfg2_reg);
  270. TSI_WRITE(TSI108_EC_PORTCTRL, portctrl_reg);
  271. }
  272. if (data->link_up == 0) {
  273. /* The manual says it can take 3-4 usecs for the speed change
  274. * to take effect.
  275. */
  276. udelay(5);
  277. spin_lock(&data->txlock);
  278. if (is_valid_ether_addr(dev->dev_addr) && data->txfree)
  279. netif_wake_queue(dev);
  280. data->link_up = 1;
  281. spin_unlock(&data->txlock);
  282. }
  283. } else {
  284. if (data->link_up == 1) {
  285. netif_stop_queue(dev);
  286. data->link_up = 0;
  287. printk(KERN_NOTICE "%s : link is down\n", dev->name);
  288. }
  289. goto out;
  290. }
  291. out:
  292. spin_unlock_irqrestore(&phy_lock, flags);
  293. }
  294. static inline void
  295. tsi108_stat_carry_one(int carry, int carry_bit, int carry_shift,
  296. unsigned long *upper)
  297. {
  298. if (carry & carry_bit)
  299. *upper += carry_shift;
  300. }
  301. static void tsi108_stat_carry(struct net_device *dev)
  302. {
  303. struct tsi108_prv_data *data = netdev_priv(dev);
  304. u32 carry1, carry2;
  305. spin_lock_irq(&data->misclock);
  306. carry1 = TSI_READ(TSI108_STAT_CARRY1);
  307. carry2 = TSI_READ(TSI108_STAT_CARRY2);
  308. TSI_WRITE(TSI108_STAT_CARRY1, carry1);
  309. TSI_WRITE(TSI108_STAT_CARRY2, carry2);
  310. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXBYTES,
  311. TSI108_STAT_RXBYTES_CARRY, &data->stats.rx_bytes);
  312. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXPKTS,
  313. TSI108_STAT_RXPKTS_CARRY,
  314. &data->stats.rx_packets);
  315. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXFCS,
  316. TSI108_STAT_RXFCS_CARRY, &data->rx_fcs);
  317. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXMCAST,
  318. TSI108_STAT_RXMCAST_CARRY,
  319. &data->stats.multicast);
  320. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXALIGN,
  321. TSI108_STAT_RXALIGN_CARRY,
  322. &data->stats.rx_frame_errors);
  323. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXLENGTH,
  324. TSI108_STAT_RXLENGTH_CARRY,
  325. &data->stats.rx_length_errors);
  326. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXRUNT,
  327. TSI108_STAT_RXRUNT_CARRY, &data->rx_underruns);
  328. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXJUMBO,
  329. TSI108_STAT_RXJUMBO_CARRY, &data->rx_overruns);
  330. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXFRAG,
  331. TSI108_STAT_RXFRAG_CARRY, &data->rx_short_fcs);
  332. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXJABBER,
  333. TSI108_STAT_RXJABBER_CARRY, &data->rx_long_fcs);
  334. tsi108_stat_carry_one(carry1, TSI108_STAT_CARRY1_RXDROP,
  335. TSI108_STAT_RXDROP_CARRY,
  336. &data->stats.rx_missed_errors);
  337. tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXBYTES,
  338. TSI108_STAT_TXBYTES_CARRY, &data->stats.tx_bytes);
  339. tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXPKTS,
  340. TSI108_STAT_TXPKTS_CARRY,
  341. &data->stats.tx_packets);
  342. tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXEXDEF,
  343. TSI108_STAT_TXEXDEF_CARRY,
  344. &data->stats.tx_aborted_errors);
  345. tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXEXCOL,
  346. TSI108_STAT_TXEXCOL_CARRY, &data->tx_coll_abort);
  347. tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXTCOL,
  348. TSI108_STAT_TXTCOL_CARRY,
  349. &data->stats.collisions);
  350. tsi108_stat_carry_one(carry2, TSI108_STAT_CARRY2_TXPAUSE,
  351. TSI108_STAT_TXPAUSEDROP_CARRY,
  352. &data->tx_pause_drop);
  353. spin_unlock_irq(&data->misclock);
  354. }
  355. /* Read a stat counter atomically with respect to carries.
  356. * data->misclock must be held.
  357. */
  358. static inline unsigned long
  359. tsi108_read_stat(struct tsi108_prv_data * data, int reg, int carry_bit,
  360. int carry_shift, unsigned long *upper)
  361. {
  362. int carryreg;
  363. unsigned long val;
  364. if (reg < 0xb0)
  365. carryreg = TSI108_STAT_CARRY1;
  366. else
  367. carryreg = TSI108_STAT_CARRY2;
  368. again:
  369. val = TSI_READ(reg) | *upper;
  370. /* Check to see if it overflowed, but the interrupt hasn't
  371. * been serviced yet. If so, handle the carry here, and
  372. * try again.
  373. */
  374. if (unlikely(TSI_READ(carryreg) & carry_bit)) {
  375. *upper += carry_shift;
  376. TSI_WRITE(carryreg, carry_bit);
  377. goto again;
  378. }
  379. return val;
  380. }
  381. static struct net_device_stats *tsi108_get_stats(struct net_device *dev)
  382. {
  383. unsigned long excol;
  384. struct tsi108_prv_data *data = netdev_priv(dev);
  385. spin_lock_irq(&data->misclock);
  386. data->tmpstats.rx_packets =
  387. tsi108_read_stat(data, TSI108_STAT_RXPKTS,
  388. TSI108_STAT_CARRY1_RXPKTS,
  389. TSI108_STAT_RXPKTS_CARRY, &data->stats.rx_packets);
  390. data->tmpstats.tx_packets =
  391. tsi108_read_stat(data, TSI108_STAT_TXPKTS,
  392. TSI108_STAT_CARRY2_TXPKTS,
  393. TSI108_STAT_TXPKTS_CARRY, &data->stats.tx_packets);
  394. data->tmpstats.rx_bytes =
  395. tsi108_read_stat(data, TSI108_STAT_RXBYTES,
  396. TSI108_STAT_CARRY1_RXBYTES,
  397. TSI108_STAT_RXBYTES_CARRY, &data->stats.rx_bytes);
  398. data->tmpstats.tx_bytes =
  399. tsi108_read_stat(data, TSI108_STAT_TXBYTES,
  400. TSI108_STAT_CARRY2_TXBYTES,
  401. TSI108_STAT_TXBYTES_CARRY, &data->stats.tx_bytes);
  402. data->tmpstats.multicast =
  403. tsi108_read_stat(data, TSI108_STAT_RXMCAST,
  404. TSI108_STAT_CARRY1_RXMCAST,
  405. TSI108_STAT_RXMCAST_CARRY, &data->stats.multicast);
  406. excol = tsi108_read_stat(data, TSI108_STAT_TXEXCOL,
  407. TSI108_STAT_CARRY2_TXEXCOL,
  408. TSI108_STAT_TXEXCOL_CARRY,
  409. &data->tx_coll_abort);
  410. data->tmpstats.collisions =
  411. tsi108_read_stat(data, TSI108_STAT_TXTCOL,
  412. TSI108_STAT_CARRY2_TXTCOL,
  413. TSI108_STAT_TXTCOL_CARRY, &data->stats.collisions);
  414. data->tmpstats.collisions += excol;
  415. data->tmpstats.rx_length_errors =
  416. tsi108_read_stat(data, TSI108_STAT_RXLENGTH,
  417. TSI108_STAT_CARRY1_RXLENGTH,
  418. TSI108_STAT_RXLENGTH_CARRY,
  419. &data->stats.rx_length_errors);
  420. data->tmpstats.rx_length_errors +=
  421. tsi108_read_stat(data, TSI108_STAT_RXRUNT,
  422. TSI108_STAT_CARRY1_RXRUNT,
  423. TSI108_STAT_RXRUNT_CARRY, &data->rx_underruns);
  424. data->tmpstats.rx_length_errors +=
  425. tsi108_read_stat(data, TSI108_STAT_RXJUMBO,
  426. TSI108_STAT_CARRY1_RXJUMBO,
  427. TSI108_STAT_RXJUMBO_CARRY, &data->rx_overruns);
  428. data->tmpstats.rx_frame_errors =
  429. tsi108_read_stat(data, TSI108_STAT_RXALIGN,
  430. TSI108_STAT_CARRY1_RXALIGN,
  431. TSI108_STAT_RXALIGN_CARRY,
  432. &data->stats.rx_frame_errors);
  433. data->tmpstats.rx_frame_errors +=
  434. tsi108_read_stat(data, TSI108_STAT_RXFCS,
  435. TSI108_STAT_CARRY1_RXFCS, TSI108_STAT_RXFCS_CARRY,
  436. &data->rx_fcs);
  437. data->tmpstats.rx_frame_errors +=
  438. tsi108_read_stat(data, TSI108_STAT_RXFRAG,
  439. TSI108_STAT_CARRY1_RXFRAG,
  440. TSI108_STAT_RXFRAG_CARRY, &data->rx_short_fcs);
  441. data->tmpstats.rx_missed_errors =
  442. tsi108_read_stat(data, TSI108_STAT_RXDROP,
  443. TSI108_STAT_CARRY1_RXDROP,
  444. TSI108_STAT_RXDROP_CARRY,
  445. &data->stats.rx_missed_errors);
  446. /* These three are maintained by software. */
  447. data->tmpstats.rx_fifo_errors = data->stats.rx_fifo_errors;
  448. data->tmpstats.rx_crc_errors = data->stats.rx_crc_errors;
  449. data->tmpstats.tx_aborted_errors =
  450. tsi108_read_stat(data, TSI108_STAT_TXEXDEF,
  451. TSI108_STAT_CARRY2_TXEXDEF,
  452. TSI108_STAT_TXEXDEF_CARRY,
  453. &data->stats.tx_aborted_errors);
  454. data->tmpstats.tx_aborted_errors +=
  455. tsi108_read_stat(data, TSI108_STAT_TXPAUSEDROP,
  456. TSI108_STAT_CARRY2_TXPAUSE,
  457. TSI108_STAT_TXPAUSEDROP_CARRY,
  458. &data->tx_pause_drop);
  459. data->tmpstats.tx_aborted_errors += excol;
  460. data->tmpstats.tx_errors = data->tmpstats.tx_aborted_errors;
  461. data->tmpstats.rx_errors = data->tmpstats.rx_length_errors +
  462. data->tmpstats.rx_crc_errors +
  463. data->tmpstats.rx_frame_errors +
  464. data->tmpstats.rx_fifo_errors + data->tmpstats.rx_missed_errors;
  465. spin_unlock_irq(&data->misclock);
  466. return &data->tmpstats;
  467. }
  468. static void tsi108_restart_rx(struct tsi108_prv_data * data, struct net_device *dev)
  469. {
  470. TSI_WRITE(TSI108_EC_RXQ_PTRHIGH,
  471. TSI108_EC_RXQ_PTRHIGH_VALID);
  472. TSI_WRITE(TSI108_EC_RXCTRL, TSI108_EC_RXCTRL_GO
  473. | TSI108_EC_RXCTRL_QUEUE0);
  474. }
  475. static void tsi108_restart_tx(struct tsi108_prv_data * data)
  476. {
  477. TSI_WRITE(TSI108_EC_TXQ_PTRHIGH,
  478. TSI108_EC_TXQ_PTRHIGH_VALID);
  479. TSI_WRITE(TSI108_EC_TXCTRL, TSI108_EC_TXCTRL_IDLEINT |
  480. TSI108_EC_TXCTRL_GO | TSI108_EC_TXCTRL_QUEUE0);
  481. }
  482. /* txlock must be held by caller, with IRQs disabled, and
  483. * with permission to re-enable them when the lock is dropped.
  484. */
  485. static void tsi108_complete_tx(struct net_device *dev)
  486. {
  487. struct tsi108_prv_data *data = netdev_priv(dev);
  488. int tx;
  489. struct sk_buff *skb;
  490. int release = 0;
  491. while (!data->txfree || data->txhead != data->txtail) {
  492. tx = data->txtail;
  493. if (data->txring[tx].misc & TSI108_TX_OWN)
  494. break;
  495. skb = data->txskbs[tx];
  496. if (!(data->txring[tx].misc & TSI108_TX_OK))
  497. printk("%s: bad tx packet, misc %x\n",
  498. dev->name, data->txring[tx].misc);
  499. data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
  500. data->txfree++;
  501. if (data->txring[tx].misc & TSI108_TX_EOF) {
  502. dev_kfree_skb_any(skb);
  503. release++;
  504. }
  505. }
  506. if (release) {
  507. if (is_valid_ether_addr(dev->dev_addr) && data->link_up)
  508. netif_wake_queue(dev);
  509. }
  510. }
  511. static int tsi108_send_packet(struct sk_buff * skb, struct net_device *dev)
  512. {
  513. struct tsi108_prv_data *data = netdev_priv(dev);
  514. int frags = skb_shinfo(skb)->nr_frags + 1;
  515. int i;
  516. if (!data->phy_ok && net_ratelimit())
  517. printk(KERN_ERR "%s: Transmit while PHY is down!\n", dev->name);
  518. if (!data->link_up) {
  519. printk(KERN_ERR "%s: Transmit while link is down!\n",
  520. dev->name);
  521. netif_stop_queue(dev);
  522. return NETDEV_TX_BUSY;
  523. }
  524. if (data->txfree < MAX_SKB_FRAGS + 1) {
  525. netif_stop_queue(dev);
  526. if (net_ratelimit())
  527. printk(KERN_ERR "%s: Transmit with full tx ring!\n",
  528. dev->name);
  529. return NETDEV_TX_BUSY;
  530. }
  531. if (data->txfree - frags < MAX_SKB_FRAGS + 1) {
  532. netif_stop_queue(dev);
  533. }
  534. spin_lock_irq(&data->txlock);
  535. for (i = 0; i < frags; i++) {
  536. int misc = 0;
  537. int tx = data->txhead;
  538. /* This is done to mark every TSI108_TX_INT_FREQ tx buffers with
  539. * the interrupt bit. TX descriptor-complete interrupts are
  540. * enabled when the queue fills up, and masked when there is
  541. * still free space. This way, when saturating the outbound
  542. * link, the tx interrupts are kept to a reasonable level.
  543. * When the queue is not full, reclamation of skbs still occurs
  544. * as new packets are transmitted, or on a queue-empty
  545. * interrupt.
  546. */
  547. if ((tx % TSI108_TX_INT_FREQ == 0) &&
  548. ((TSI108_TXRING_LEN - data->txfree) >= TSI108_TX_INT_FREQ))
  549. misc = TSI108_TX_INT;
  550. data->txskbs[tx] = skb;
  551. if (i == 0) {
  552. data->txring[tx].buf0 = dma_map_single(NULL, skb->data,
  553. skb_headlen(skb), DMA_TO_DEVICE);
  554. data->txring[tx].len = skb_headlen(skb);
  555. misc |= TSI108_TX_SOF;
  556. } else {
  557. skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
  558. data->txring[tx].buf0 =
  559. dma_map_page(NULL, frag->page, frag->page_offset,
  560. frag->size, DMA_TO_DEVICE);
  561. data->txring[tx].len = frag->size;
  562. }
  563. if (i == frags - 1)
  564. misc |= TSI108_TX_EOF;
  565. if (netif_msg_pktdata(data)) {
  566. int i;
  567. printk("%s: Tx Frame contents (%d)\n", dev->name,
  568. skb->len);
  569. for (i = 0; i < skb->len; i++)
  570. printk(" %2.2x", skb->data[i]);
  571. printk(".\n");
  572. }
  573. data->txring[tx].misc = misc | TSI108_TX_OWN;
  574. data->txhead = (data->txhead + 1) % TSI108_TXRING_LEN;
  575. data->txfree--;
  576. }
  577. tsi108_complete_tx(dev);
  578. /* This must be done after the check for completed tx descriptors,
  579. * so that the tail pointer is correct.
  580. */
  581. if (!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_QUEUE0))
  582. tsi108_restart_tx(data);
  583. spin_unlock_irq(&data->txlock);
  584. return NETDEV_TX_OK;
  585. }
  586. static int tsi108_complete_rx(struct net_device *dev, int budget)
  587. {
  588. struct tsi108_prv_data *data = netdev_priv(dev);
  589. int done = 0;
  590. while (data->rxfree && done != budget) {
  591. int rx = data->rxtail;
  592. struct sk_buff *skb;
  593. if (data->rxring[rx].misc & TSI108_RX_OWN)
  594. break;
  595. skb = data->rxskbs[rx];
  596. data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
  597. data->rxfree--;
  598. done++;
  599. if (data->rxring[rx].misc & TSI108_RX_BAD) {
  600. spin_lock_irq(&data->misclock);
  601. if (data->rxring[rx].misc & TSI108_RX_CRC)
  602. data->stats.rx_crc_errors++;
  603. if (data->rxring[rx].misc & TSI108_RX_OVER)
  604. data->stats.rx_fifo_errors++;
  605. spin_unlock_irq(&data->misclock);
  606. dev_kfree_skb_any(skb);
  607. continue;
  608. }
  609. if (netif_msg_pktdata(data)) {
  610. int i;
  611. printk("%s: Rx Frame contents (%d)\n",
  612. dev->name, data->rxring[rx].len);
  613. for (i = 0; i < data->rxring[rx].len; i++)
  614. printk(" %2.2x", skb->data[i]);
  615. printk(".\n");
  616. }
  617. skb_put(skb, data->rxring[rx].len);
  618. skb->protocol = eth_type_trans(skb, dev);
  619. netif_receive_skb(skb);
  620. }
  621. return done;
  622. }
  623. static int tsi108_refill_rx(struct net_device *dev, int budget)
  624. {
  625. struct tsi108_prv_data *data = netdev_priv(dev);
  626. int done = 0;
  627. while (data->rxfree != TSI108_RXRING_LEN && done != budget) {
  628. int rx = data->rxhead;
  629. struct sk_buff *skb;
  630. skb = netdev_alloc_skb_ip_align(dev, TSI108_RXBUF_SIZE);
  631. data->rxskbs[rx] = skb;
  632. if (!skb)
  633. break;
  634. data->rxring[rx].buf0 = dma_map_single(NULL, skb->data,
  635. TSI108_RX_SKB_SIZE,
  636. DMA_FROM_DEVICE);
  637. /* Sometimes the hardware sets blen to zero after packet
  638. * reception, even though the manual says that it's only ever
  639. * modified by the driver.
  640. */
  641. data->rxring[rx].blen = TSI108_RX_SKB_SIZE;
  642. data->rxring[rx].misc = TSI108_RX_OWN | TSI108_RX_INT;
  643. data->rxhead = (data->rxhead + 1) % TSI108_RXRING_LEN;
  644. data->rxfree++;
  645. done++;
  646. }
  647. if (done != 0 && !(TSI_READ(TSI108_EC_RXSTAT) &
  648. TSI108_EC_RXSTAT_QUEUE0))
  649. tsi108_restart_rx(data, dev);
  650. return done;
  651. }
  652. static int tsi108_poll(struct napi_struct *napi, int budget)
  653. {
  654. struct tsi108_prv_data *data = container_of(napi, struct tsi108_prv_data, napi);
  655. struct net_device *dev = data->dev;
  656. u32 estat = TSI_READ(TSI108_EC_RXESTAT);
  657. u32 intstat = TSI_READ(TSI108_EC_INTSTAT);
  658. int num_received = 0, num_filled = 0;
  659. intstat &= TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
  660. TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR | TSI108_INT_RXWAIT;
  661. TSI_WRITE(TSI108_EC_RXESTAT, estat);
  662. TSI_WRITE(TSI108_EC_INTSTAT, intstat);
  663. if (data->rxpending || (estat & TSI108_EC_RXESTAT_Q0_DESCINT))
  664. num_received = tsi108_complete_rx(dev, budget);
  665. /* This should normally fill no more slots than the number of
  666. * packets received in tsi108_complete_rx(). The exception
  667. * is when we previously ran out of memory for RX SKBs. In that
  668. * case, it's helpful to obey the budget, not only so that the
  669. * CPU isn't hogged, but so that memory (which may still be low)
  670. * is not hogged by one device.
  671. *
  672. * A work unit is considered to be two SKBs to allow us to catch
  673. * up when the ring has shrunk due to out-of-memory but we're
  674. * still removing the full budget's worth of packets each time.
  675. */
  676. if (data->rxfree < TSI108_RXRING_LEN)
  677. num_filled = tsi108_refill_rx(dev, budget * 2);
  678. if (intstat & TSI108_INT_RXERROR) {
  679. u32 err = TSI_READ(TSI108_EC_RXERR);
  680. TSI_WRITE(TSI108_EC_RXERR, err);
  681. if (err) {
  682. if (net_ratelimit())
  683. printk(KERN_DEBUG "%s: RX error %x\n",
  684. dev->name, err);
  685. if (!(TSI_READ(TSI108_EC_RXSTAT) &
  686. TSI108_EC_RXSTAT_QUEUE0))
  687. tsi108_restart_rx(data, dev);
  688. }
  689. }
  690. if (intstat & TSI108_INT_RXOVERRUN) {
  691. spin_lock_irq(&data->misclock);
  692. data->stats.rx_fifo_errors++;
  693. spin_unlock_irq(&data->misclock);
  694. }
  695. if (num_received < budget) {
  696. data->rxpending = 0;
  697. napi_complete(napi);
  698. TSI_WRITE(TSI108_EC_INTMASK,
  699. TSI_READ(TSI108_EC_INTMASK)
  700. & ~(TSI108_INT_RXQUEUE0
  701. | TSI108_INT_RXTHRESH |
  702. TSI108_INT_RXOVERRUN |
  703. TSI108_INT_RXERROR |
  704. TSI108_INT_RXWAIT));
  705. } else {
  706. data->rxpending = 1;
  707. }
  708. return num_received;
  709. }
  710. static void tsi108_rx_int(struct net_device *dev)
  711. {
  712. struct tsi108_prv_data *data = netdev_priv(dev);
  713. /* A race could cause dev to already be scheduled, so it's not an
  714. * error if that happens (and interrupts shouldn't be re-masked,
  715. * because that can cause harmful races, if poll has already
  716. * unmasked them but not cleared LINK_STATE_SCHED).
  717. *
  718. * This can happen if this code races with tsi108_poll(), which masks
  719. * the interrupts after tsi108_irq_one() read the mask, but before
  720. * napi_schedule is called. It could also happen due to calls
  721. * from tsi108_check_rxring().
  722. */
  723. if (napi_schedule_prep(&data->napi)) {
  724. /* Mask, rather than ack, the receive interrupts. The ack
  725. * will happen in tsi108_poll().
  726. */
  727. TSI_WRITE(TSI108_EC_INTMASK,
  728. TSI_READ(TSI108_EC_INTMASK) |
  729. TSI108_INT_RXQUEUE0
  730. | TSI108_INT_RXTHRESH |
  731. TSI108_INT_RXOVERRUN | TSI108_INT_RXERROR |
  732. TSI108_INT_RXWAIT);
  733. __napi_schedule(&data->napi);
  734. } else {
  735. if (!netif_running(dev)) {
  736. /* This can happen if an interrupt occurs while the
  737. * interface is being brought down, as the START
  738. * bit is cleared before the stop function is called.
  739. *
  740. * In this case, the interrupts must be masked, or
  741. * they will continue indefinitely.
  742. *
  743. * There's a race here if the interface is brought down
  744. * and then up in rapid succession, as the device could
  745. * be made running after the above check and before
  746. * the masking below. This will only happen if the IRQ
  747. * thread has a lower priority than the task brining
  748. * up the interface. Fixing this race would likely
  749. * require changes in generic code.
  750. */
  751. TSI_WRITE(TSI108_EC_INTMASK,
  752. TSI_READ
  753. (TSI108_EC_INTMASK) |
  754. TSI108_INT_RXQUEUE0 |
  755. TSI108_INT_RXTHRESH |
  756. TSI108_INT_RXOVERRUN |
  757. TSI108_INT_RXERROR |
  758. TSI108_INT_RXWAIT);
  759. }
  760. }
  761. }
  762. /* If the RX ring has run out of memory, try periodically
  763. * to allocate some more, as otherwise poll would never
  764. * get called (apart from the initial end-of-queue condition).
  765. *
  766. * This is called once per second (by default) from the thread.
  767. */
  768. static void tsi108_check_rxring(struct net_device *dev)
  769. {
  770. struct tsi108_prv_data *data = netdev_priv(dev);
  771. /* A poll is scheduled, as opposed to caling tsi108_refill_rx
  772. * directly, so as to keep the receive path single-threaded
  773. * (and thus not needing a lock).
  774. */
  775. if (netif_running(dev) && data->rxfree < TSI108_RXRING_LEN / 4)
  776. tsi108_rx_int(dev);
  777. }
  778. static void tsi108_tx_int(struct net_device *dev)
  779. {
  780. struct tsi108_prv_data *data = netdev_priv(dev);
  781. u32 estat = TSI_READ(TSI108_EC_TXESTAT);
  782. TSI_WRITE(TSI108_EC_TXESTAT, estat);
  783. TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_TXQUEUE0 |
  784. TSI108_INT_TXIDLE | TSI108_INT_TXERROR);
  785. if (estat & TSI108_EC_TXESTAT_Q0_ERR) {
  786. u32 err = TSI_READ(TSI108_EC_TXERR);
  787. TSI_WRITE(TSI108_EC_TXERR, err);
  788. if (err && net_ratelimit())
  789. printk(KERN_ERR "%s: TX error %x\n", dev->name, err);
  790. }
  791. if (estat & (TSI108_EC_TXESTAT_Q0_DESCINT | TSI108_EC_TXESTAT_Q0_EOQ)) {
  792. spin_lock(&data->txlock);
  793. tsi108_complete_tx(dev);
  794. spin_unlock(&data->txlock);
  795. }
  796. }
  797. static irqreturn_t tsi108_irq(int irq, void *dev_id)
  798. {
  799. struct net_device *dev = dev_id;
  800. struct tsi108_prv_data *data = netdev_priv(dev);
  801. u32 stat = TSI_READ(TSI108_EC_INTSTAT);
  802. if (!(stat & TSI108_INT_ANY))
  803. return IRQ_NONE; /* Not our interrupt */
  804. stat &= ~TSI_READ(TSI108_EC_INTMASK);
  805. if (stat & (TSI108_INT_TXQUEUE0 | TSI108_INT_TXIDLE |
  806. TSI108_INT_TXERROR))
  807. tsi108_tx_int(dev);
  808. if (stat & (TSI108_INT_RXQUEUE0 | TSI108_INT_RXTHRESH |
  809. TSI108_INT_RXWAIT | TSI108_INT_RXOVERRUN |
  810. TSI108_INT_RXERROR))
  811. tsi108_rx_int(dev);
  812. if (stat & TSI108_INT_SFN) {
  813. if (net_ratelimit())
  814. printk(KERN_DEBUG "%s: SFN error\n", dev->name);
  815. TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_SFN);
  816. }
  817. if (stat & TSI108_INT_STATCARRY) {
  818. tsi108_stat_carry(dev);
  819. TSI_WRITE(TSI108_EC_INTSTAT, TSI108_INT_STATCARRY);
  820. }
  821. return IRQ_HANDLED;
  822. }
  823. static void tsi108_stop_ethernet(struct net_device *dev)
  824. {
  825. struct tsi108_prv_data *data = netdev_priv(dev);
  826. int i = 1000;
  827. /* Disable all TX and RX queues ... */
  828. TSI_WRITE(TSI108_EC_TXCTRL, 0);
  829. TSI_WRITE(TSI108_EC_RXCTRL, 0);
  830. /* ...and wait for them to become idle */
  831. while(i--) {
  832. if(!(TSI_READ(TSI108_EC_TXSTAT) & TSI108_EC_TXSTAT_ACTIVE))
  833. break;
  834. udelay(10);
  835. }
  836. i = 1000;
  837. while(i--){
  838. if(!(TSI_READ(TSI108_EC_RXSTAT) & TSI108_EC_RXSTAT_ACTIVE))
  839. return;
  840. udelay(10);
  841. }
  842. printk(KERN_ERR "%s function time out\n", __func__);
  843. }
  844. static void tsi108_reset_ether(struct tsi108_prv_data * data)
  845. {
  846. TSI_WRITE(TSI108_MAC_CFG1, TSI108_MAC_CFG1_SOFTRST);
  847. udelay(100);
  848. TSI_WRITE(TSI108_MAC_CFG1, 0);
  849. TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATRST);
  850. udelay(100);
  851. TSI_WRITE(TSI108_EC_PORTCTRL,
  852. TSI_READ(TSI108_EC_PORTCTRL) &
  853. ~TSI108_EC_PORTCTRL_STATRST);
  854. TSI_WRITE(TSI108_EC_TXCFG, TSI108_EC_TXCFG_RST);
  855. udelay(100);
  856. TSI_WRITE(TSI108_EC_TXCFG,
  857. TSI_READ(TSI108_EC_TXCFG) &
  858. ~TSI108_EC_TXCFG_RST);
  859. TSI_WRITE(TSI108_EC_RXCFG, TSI108_EC_RXCFG_RST);
  860. udelay(100);
  861. TSI_WRITE(TSI108_EC_RXCFG,
  862. TSI_READ(TSI108_EC_RXCFG) &
  863. ~TSI108_EC_RXCFG_RST);
  864. TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
  865. TSI_READ(TSI108_MAC_MII_MGMT_CFG) |
  866. TSI108_MAC_MII_MGMT_RST);
  867. udelay(100);
  868. TSI_WRITE(TSI108_MAC_MII_MGMT_CFG,
  869. (TSI_READ(TSI108_MAC_MII_MGMT_CFG) &
  870. ~(TSI108_MAC_MII_MGMT_RST |
  871. TSI108_MAC_MII_MGMT_CLK)) | 0x07);
  872. }
  873. static int tsi108_get_mac(struct net_device *dev)
  874. {
  875. struct tsi108_prv_data *data = netdev_priv(dev);
  876. u32 word1 = TSI_READ(TSI108_MAC_ADDR1);
  877. u32 word2 = TSI_READ(TSI108_MAC_ADDR2);
  878. /* Note that the octets are reversed from what the manual says,
  879. * producing an even weirder ordering...
  880. */
  881. if (word2 == 0 && word1 == 0) {
  882. dev->dev_addr[0] = 0x00;
  883. dev->dev_addr[1] = 0x06;
  884. dev->dev_addr[2] = 0xd2;
  885. dev->dev_addr[3] = 0x00;
  886. dev->dev_addr[4] = 0x00;
  887. if (0x8 == data->phy)
  888. dev->dev_addr[5] = 0x01;
  889. else
  890. dev->dev_addr[5] = 0x02;
  891. word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
  892. word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
  893. (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
  894. TSI_WRITE(TSI108_MAC_ADDR1, word1);
  895. TSI_WRITE(TSI108_MAC_ADDR2, word2);
  896. } else {
  897. dev->dev_addr[0] = (word2 >> 16) & 0xff;
  898. dev->dev_addr[1] = (word2 >> 24) & 0xff;
  899. dev->dev_addr[2] = (word1 >> 0) & 0xff;
  900. dev->dev_addr[3] = (word1 >> 8) & 0xff;
  901. dev->dev_addr[4] = (word1 >> 16) & 0xff;
  902. dev->dev_addr[5] = (word1 >> 24) & 0xff;
  903. }
  904. if (!is_valid_ether_addr(dev->dev_addr)) {
  905. printk(KERN_ERR
  906. "%s: Invalid MAC address. word1: %08x, word2: %08x\n",
  907. dev->name, word1, word2);
  908. return -EINVAL;
  909. }
  910. return 0;
  911. }
  912. static int tsi108_set_mac(struct net_device *dev, void *addr)
  913. {
  914. struct tsi108_prv_data *data = netdev_priv(dev);
  915. u32 word1, word2;
  916. int i;
  917. if (!is_valid_ether_addr(addr))
  918. return -EINVAL;
  919. for (i = 0; i < 6; i++)
  920. /* +2 is for the offset of the HW addr type */
  921. dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
  922. word2 = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 24);
  923. word1 = (dev->dev_addr[2] << 0) | (dev->dev_addr[3] << 8) |
  924. (dev->dev_addr[4] << 16) | (dev->dev_addr[5] << 24);
  925. spin_lock_irq(&data->misclock);
  926. TSI_WRITE(TSI108_MAC_ADDR1, word1);
  927. TSI_WRITE(TSI108_MAC_ADDR2, word2);
  928. spin_lock(&data->txlock);
  929. if (data->txfree && data->link_up)
  930. netif_wake_queue(dev);
  931. spin_unlock(&data->txlock);
  932. spin_unlock_irq(&data->misclock);
  933. return 0;
  934. }
  935. /* Protected by dev->xmit_lock. */
  936. static void tsi108_set_rx_mode(struct net_device *dev)
  937. {
  938. struct tsi108_prv_data *data = netdev_priv(dev);
  939. u32 rxcfg = TSI_READ(TSI108_EC_RXCFG);
  940. if (dev->flags & IFF_PROMISC) {
  941. rxcfg &= ~(TSI108_EC_RXCFG_UC_HASH | TSI108_EC_RXCFG_MC_HASH);
  942. rxcfg |= TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE;
  943. goto out;
  944. }
  945. rxcfg &= ~(TSI108_EC_RXCFG_UFE | TSI108_EC_RXCFG_MFE);
  946. if (dev->flags & IFF_ALLMULTI || !netdev_mc_empty(dev)) {
  947. int i;
  948. struct netdev_hw_addr *ha;
  949. rxcfg |= TSI108_EC_RXCFG_MFE | TSI108_EC_RXCFG_MC_HASH;
  950. memset(data->mc_hash, 0, sizeof(data->mc_hash));
  951. netdev_for_each_mc_addr(ha, dev) {
  952. u32 hash, crc;
  953. crc = ether_crc(6, ha->addr);
  954. hash = crc >> 23;
  955. __set_bit(hash, &data->mc_hash[0]);
  956. }
  957. TSI_WRITE(TSI108_EC_HASHADDR,
  958. TSI108_EC_HASHADDR_AUTOINC |
  959. TSI108_EC_HASHADDR_MCAST);
  960. for (i = 0; i < 16; i++) {
  961. /* The manual says that the hardware may drop
  962. * back-to-back writes to the data register.
  963. */
  964. udelay(1);
  965. TSI_WRITE(TSI108_EC_HASHDATA,
  966. data->mc_hash[i]);
  967. }
  968. }
  969. out:
  970. TSI_WRITE(TSI108_EC_RXCFG, rxcfg);
  971. }
  972. static void tsi108_init_phy(struct net_device *dev)
  973. {
  974. struct tsi108_prv_data *data = netdev_priv(dev);
  975. u32 i = 0;
  976. u16 phyval = 0;
  977. unsigned long flags;
  978. spin_lock_irqsave(&phy_lock, flags);
  979. tsi108_write_mii(data, MII_BMCR, BMCR_RESET);
  980. while (--i) {
  981. if(!(tsi108_read_mii(data, MII_BMCR) & BMCR_RESET))
  982. break;
  983. udelay(10);
  984. }
  985. if (i == 0)
  986. printk(KERN_ERR "%s function time out\n", __func__);
  987. if (data->phy_type == TSI108_PHY_BCM54XX) {
  988. tsi108_write_mii(data, 0x09, 0x0300);
  989. tsi108_write_mii(data, 0x10, 0x1020);
  990. tsi108_write_mii(data, 0x1c, 0x8c00);
  991. }
  992. tsi108_write_mii(data,
  993. MII_BMCR,
  994. BMCR_ANENABLE | BMCR_ANRESTART);
  995. while (tsi108_read_mii(data, MII_BMCR) & BMCR_ANRESTART)
  996. cpu_relax();
  997. /* Set G/MII mode and receive clock select in TBI control #2. The
  998. * second port won't work if this isn't done, even though we don't
  999. * use TBI mode.
  1000. */
  1001. tsi108_write_tbi(data, 0x11, 0x30);
  1002. /* FIXME: It seems to take more than 2 back-to-back reads to the
  1003. * PHY_STAT register before the link up status bit is set.
  1004. */
  1005. data->link_up = 0;
  1006. while (!((phyval = tsi108_read_mii(data, MII_BMSR)) &
  1007. BMSR_LSTATUS)) {
  1008. if (i++ > (MII_READ_DELAY / 10)) {
  1009. break;
  1010. }
  1011. spin_unlock_irqrestore(&phy_lock, flags);
  1012. msleep(10);
  1013. spin_lock_irqsave(&phy_lock, flags);
  1014. }
  1015. data->mii_if.supports_gmii = mii_check_gmii_support(&data->mii_if);
  1016. printk(KERN_DEBUG "PHY_STAT reg contains %08x\n", phyval);
  1017. data->phy_ok = 1;
  1018. data->init_media = 1;
  1019. spin_unlock_irqrestore(&phy_lock, flags);
  1020. }
  1021. static void tsi108_kill_phy(struct net_device *dev)
  1022. {
  1023. struct tsi108_prv_data *data = netdev_priv(dev);
  1024. unsigned long flags;
  1025. spin_lock_irqsave(&phy_lock, flags);
  1026. tsi108_write_mii(data, MII_BMCR, BMCR_PDOWN);
  1027. data->phy_ok = 0;
  1028. spin_unlock_irqrestore(&phy_lock, flags);
  1029. }
  1030. static int tsi108_open(struct net_device *dev)
  1031. {
  1032. int i;
  1033. struct tsi108_prv_data *data = netdev_priv(dev);
  1034. unsigned int rxring_size = TSI108_RXRING_LEN * sizeof(rx_desc);
  1035. unsigned int txring_size = TSI108_TXRING_LEN * sizeof(tx_desc);
  1036. i = request_irq(data->irq_num, tsi108_irq, 0, dev->name, dev);
  1037. if (i != 0) {
  1038. printk(KERN_ERR "tsi108_eth%d: Could not allocate IRQ%d.\n",
  1039. data->id, data->irq_num);
  1040. return i;
  1041. } else {
  1042. dev->irq = data->irq_num;
  1043. printk(KERN_NOTICE
  1044. "tsi108_open : Port %d Assigned IRQ %d to %s\n",
  1045. data->id, dev->irq, dev->name);
  1046. }
  1047. data->rxring = dma_alloc_coherent(NULL, rxring_size,
  1048. &data->rxdma, GFP_KERNEL);
  1049. if (!data->rxring) {
  1050. printk(KERN_DEBUG
  1051. "TSI108_ETH: failed to allocate memory for rxring!\n");
  1052. return -ENOMEM;
  1053. } else {
  1054. memset(data->rxring, 0, rxring_size);
  1055. }
  1056. data->txring = dma_alloc_coherent(NULL, txring_size,
  1057. &data->txdma, GFP_KERNEL);
  1058. if (!data->txring) {
  1059. printk(KERN_DEBUG
  1060. "TSI108_ETH: failed to allocate memory for txring!\n");
  1061. pci_free_consistent(0, rxring_size, data->rxring, data->rxdma);
  1062. return -ENOMEM;
  1063. } else {
  1064. memset(data->txring, 0, txring_size);
  1065. }
  1066. for (i = 0; i < TSI108_RXRING_LEN; i++) {
  1067. data->rxring[i].next0 = data->rxdma + (i + 1) * sizeof(rx_desc);
  1068. data->rxring[i].blen = TSI108_RXBUF_SIZE;
  1069. data->rxring[i].vlan = 0;
  1070. }
  1071. data->rxring[TSI108_RXRING_LEN - 1].next0 = data->rxdma;
  1072. data->rxtail = 0;
  1073. data->rxhead = 0;
  1074. for (i = 0; i < TSI108_RXRING_LEN; i++) {
  1075. struct sk_buff *skb;
  1076. skb = netdev_alloc_skb_ip_align(dev, TSI108_RXBUF_SIZE);
  1077. if (!skb) {
  1078. /* Bah. No memory for now, but maybe we'll get
  1079. * some more later.
  1080. * For now, we'll live with the smaller ring.
  1081. */
  1082. printk(KERN_WARNING
  1083. "%s: Could only allocate %d receive skb(s).\n",
  1084. dev->name, i);
  1085. data->rxhead = i;
  1086. break;
  1087. }
  1088. data->rxskbs[i] = skb;
  1089. data->rxskbs[i] = skb;
  1090. data->rxring[i].buf0 = virt_to_phys(data->rxskbs[i]->data);
  1091. data->rxring[i].misc = TSI108_RX_OWN | TSI108_RX_INT;
  1092. }
  1093. data->rxfree = i;
  1094. TSI_WRITE(TSI108_EC_RXQ_PTRLOW, data->rxdma);
  1095. for (i = 0; i < TSI108_TXRING_LEN; i++) {
  1096. data->txring[i].next0 = data->txdma + (i + 1) * sizeof(tx_desc);
  1097. data->txring[i].misc = 0;
  1098. }
  1099. data->txring[TSI108_TXRING_LEN - 1].next0 = data->txdma;
  1100. data->txtail = 0;
  1101. data->txhead = 0;
  1102. data->txfree = TSI108_TXRING_LEN;
  1103. TSI_WRITE(TSI108_EC_TXQ_PTRLOW, data->txdma);
  1104. tsi108_init_phy(dev);
  1105. napi_enable(&data->napi);
  1106. setup_timer(&data->timer, tsi108_timed_checker, (unsigned long)dev);
  1107. mod_timer(&data->timer, jiffies + 1);
  1108. tsi108_restart_rx(data, dev);
  1109. TSI_WRITE(TSI108_EC_INTSTAT, ~0);
  1110. TSI_WRITE(TSI108_EC_INTMASK,
  1111. ~(TSI108_INT_TXQUEUE0 | TSI108_INT_RXERROR |
  1112. TSI108_INT_RXTHRESH | TSI108_INT_RXQUEUE0 |
  1113. TSI108_INT_RXOVERRUN | TSI108_INT_RXWAIT |
  1114. TSI108_INT_SFN | TSI108_INT_STATCARRY));
  1115. TSI_WRITE(TSI108_MAC_CFG1,
  1116. TSI108_MAC_CFG1_RXEN | TSI108_MAC_CFG1_TXEN);
  1117. netif_start_queue(dev);
  1118. return 0;
  1119. }
  1120. static int tsi108_close(struct net_device *dev)
  1121. {
  1122. struct tsi108_prv_data *data = netdev_priv(dev);
  1123. netif_stop_queue(dev);
  1124. napi_disable(&data->napi);
  1125. del_timer_sync(&data->timer);
  1126. tsi108_stop_ethernet(dev);
  1127. tsi108_kill_phy(dev);
  1128. TSI_WRITE(TSI108_EC_INTMASK, ~0);
  1129. TSI_WRITE(TSI108_MAC_CFG1, 0);
  1130. /* Check for any pending TX packets, and drop them. */
  1131. while (!data->txfree || data->txhead != data->txtail) {
  1132. int tx = data->txtail;
  1133. struct sk_buff *skb;
  1134. skb = data->txskbs[tx];
  1135. data->txtail = (data->txtail + 1) % TSI108_TXRING_LEN;
  1136. data->txfree++;
  1137. dev_kfree_skb(skb);
  1138. }
  1139. free_irq(data->irq_num, dev);
  1140. /* Discard the RX ring. */
  1141. while (data->rxfree) {
  1142. int rx = data->rxtail;
  1143. struct sk_buff *skb;
  1144. skb = data->rxskbs[rx];
  1145. data->rxtail = (data->rxtail + 1) % TSI108_RXRING_LEN;
  1146. data->rxfree--;
  1147. dev_kfree_skb(skb);
  1148. }
  1149. dma_free_coherent(0,
  1150. TSI108_RXRING_LEN * sizeof(rx_desc),
  1151. data->rxring, data->rxdma);
  1152. dma_free_coherent(0,
  1153. TSI108_TXRING_LEN * sizeof(tx_desc),
  1154. data->txring, data->txdma);
  1155. return 0;
  1156. }
  1157. static void tsi108_init_mac(struct net_device *dev)
  1158. {
  1159. struct tsi108_prv_data *data = netdev_priv(dev);
  1160. TSI_WRITE(TSI108_MAC_CFG2, TSI108_MAC_CFG2_DFLT_PREAMBLE |
  1161. TSI108_MAC_CFG2_PADCRC);
  1162. TSI_WRITE(TSI108_EC_TXTHRESH,
  1163. (192 << TSI108_EC_TXTHRESH_STARTFILL) |
  1164. (192 << TSI108_EC_TXTHRESH_STOPFILL));
  1165. TSI_WRITE(TSI108_STAT_CARRYMASK1,
  1166. ~(TSI108_STAT_CARRY1_RXBYTES |
  1167. TSI108_STAT_CARRY1_RXPKTS |
  1168. TSI108_STAT_CARRY1_RXFCS |
  1169. TSI108_STAT_CARRY1_RXMCAST |
  1170. TSI108_STAT_CARRY1_RXALIGN |
  1171. TSI108_STAT_CARRY1_RXLENGTH |
  1172. TSI108_STAT_CARRY1_RXRUNT |
  1173. TSI108_STAT_CARRY1_RXJUMBO |
  1174. TSI108_STAT_CARRY1_RXFRAG |
  1175. TSI108_STAT_CARRY1_RXJABBER |
  1176. TSI108_STAT_CARRY1_RXDROP));
  1177. TSI_WRITE(TSI108_STAT_CARRYMASK2,
  1178. ~(TSI108_STAT_CARRY2_TXBYTES |
  1179. TSI108_STAT_CARRY2_TXPKTS |
  1180. TSI108_STAT_CARRY2_TXEXDEF |
  1181. TSI108_STAT_CARRY2_TXEXCOL |
  1182. TSI108_STAT_CARRY2_TXTCOL |
  1183. TSI108_STAT_CARRY2_TXPAUSE));
  1184. TSI_WRITE(TSI108_EC_PORTCTRL, TSI108_EC_PORTCTRL_STATEN);
  1185. TSI_WRITE(TSI108_MAC_CFG1, 0);
  1186. TSI_WRITE(TSI108_EC_RXCFG,
  1187. TSI108_EC_RXCFG_SE | TSI108_EC_RXCFG_BFE);
  1188. TSI_WRITE(TSI108_EC_TXQ_CFG, TSI108_EC_TXQ_CFG_DESC_INT |
  1189. TSI108_EC_TXQ_CFG_EOQ_OWN_INT |
  1190. TSI108_EC_TXQ_CFG_WSWP | (TSI108_PBM_PORT <<
  1191. TSI108_EC_TXQ_CFG_SFNPORT));
  1192. TSI_WRITE(TSI108_EC_RXQ_CFG, TSI108_EC_RXQ_CFG_DESC_INT |
  1193. TSI108_EC_RXQ_CFG_EOQ_OWN_INT |
  1194. TSI108_EC_RXQ_CFG_WSWP | (TSI108_PBM_PORT <<
  1195. TSI108_EC_RXQ_CFG_SFNPORT));
  1196. TSI_WRITE(TSI108_EC_TXQ_BUFCFG,
  1197. TSI108_EC_TXQ_BUFCFG_BURST256 |
  1198. TSI108_EC_TXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
  1199. TSI108_EC_TXQ_BUFCFG_SFNPORT));
  1200. TSI_WRITE(TSI108_EC_RXQ_BUFCFG,
  1201. TSI108_EC_RXQ_BUFCFG_BURST256 |
  1202. TSI108_EC_RXQ_BUFCFG_BSWP | (TSI108_PBM_PORT <<
  1203. TSI108_EC_RXQ_BUFCFG_SFNPORT));
  1204. TSI_WRITE(TSI108_EC_INTMASK, ~0);
  1205. }
  1206. static int tsi108_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1207. {
  1208. struct tsi108_prv_data *data = netdev_priv(dev);
  1209. unsigned long flags;
  1210. int rc;
  1211. spin_lock_irqsave(&data->txlock, flags);
  1212. rc = mii_ethtool_gset(&data->mii_if, cmd);
  1213. spin_unlock_irqrestore(&data->txlock, flags);
  1214. return rc;
  1215. }
  1216. static int tsi108_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1217. {
  1218. struct tsi108_prv_data *data = netdev_priv(dev);
  1219. unsigned long flags;
  1220. int rc;
  1221. spin_lock_irqsave(&data->txlock, flags);
  1222. rc = mii_ethtool_sset(&data->mii_if, cmd);
  1223. spin_unlock_irqrestore(&data->txlock, flags);
  1224. return rc;
  1225. }
  1226. static int tsi108_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1227. {
  1228. struct tsi108_prv_data *data = netdev_priv(dev);
  1229. if (!netif_running(dev))
  1230. return -EINVAL;
  1231. return generic_mii_ioctl(&data->mii_if, if_mii(rq), cmd, NULL);
  1232. }
  1233. static const struct ethtool_ops tsi108_ethtool_ops = {
  1234. .get_link = ethtool_op_get_link,
  1235. .get_settings = tsi108_get_settings,
  1236. .set_settings = tsi108_set_settings,
  1237. };
  1238. static const struct net_device_ops tsi108_netdev_ops = {
  1239. .ndo_open = tsi108_open,
  1240. .ndo_stop = tsi108_close,
  1241. .ndo_start_xmit = tsi108_send_packet,
  1242. .ndo_set_multicast_list = tsi108_set_rx_mode,
  1243. .ndo_get_stats = tsi108_get_stats,
  1244. .ndo_do_ioctl = tsi108_do_ioctl,
  1245. .ndo_set_mac_address = tsi108_set_mac,
  1246. .ndo_validate_addr = eth_validate_addr,
  1247. .ndo_change_mtu = eth_change_mtu,
  1248. };
  1249. static int
  1250. tsi108_init_one(struct platform_device *pdev)
  1251. {
  1252. struct net_device *dev = NULL;
  1253. struct tsi108_prv_data *data = NULL;
  1254. hw_info *einfo;
  1255. int err = 0;
  1256. einfo = pdev->dev.platform_data;
  1257. if (NULL == einfo) {
  1258. printk(KERN_ERR "tsi-eth %d: Missing additional data!\n",
  1259. pdev->id);
  1260. return -ENODEV;
  1261. }
  1262. /* Create an ethernet device instance */
  1263. dev = alloc_etherdev(sizeof(struct tsi108_prv_data));
  1264. if (!dev) {
  1265. printk("tsi108_eth: Could not allocate a device structure\n");
  1266. return -ENOMEM;
  1267. }
  1268. printk("tsi108_eth%d: probe...\n", pdev->id);
  1269. data = netdev_priv(dev);
  1270. data->dev = dev;
  1271. pr_debug("tsi108_eth%d:regs:phyresgs:phy:irq_num=0x%x:0x%x:0x%x:0x%x\n",
  1272. pdev->id, einfo->regs, einfo->phyregs,
  1273. einfo->phy, einfo->irq_num);
  1274. data->regs = ioremap(einfo->regs, 0x400);
  1275. if (NULL == data->regs) {
  1276. err = -ENOMEM;
  1277. goto regs_fail;
  1278. }
  1279. data->phyregs = ioremap(einfo->phyregs, 0x400);
  1280. if (NULL == data->phyregs) {
  1281. err = -ENOMEM;
  1282. goto regs_fail;
  1283. }
  1284. /* MII setup */
  1285. data->mii_if.dev = dev;
  1286. data->mii_if.mdio_read = tsi108_mdio_read;
  1287. data->mii_if.mdio_write = tsi108_mdio_write;
  1288. data->mii_if.phy_id = einfo->phy;
  1289. data->mii_if.phy_id_mask = 0x1f;
  1290. data->mii_if.reg_num_mask = 0x1f;
  1291. data->phy = einfo->phy;
  1292. data->phy_type = einfo->phy_type;
  1293. data->irq_num = einfo->irq_num;
  1294. data->id = pdev->id;
  1295. netif_napi_add(dev, &data->napi, tsi108_poll, 64);
  1296. dev->netdev_ops = &tsi108_netdev_ops;
  1297. dev->ethtool_ops = &tsi108_ethtool_ops;
  1298. /* Apparently, the Linux networking code won't use scatter-gather
  1299. * if the hardware doesn't do checksums. However, it's faster
  1300. * to checksum in place and use SG, as (among other reasons)
  1301. * the cache won't be dirtied (which then has to be flushed
  1302. * before DMA). The checksumming is done by the driver (via
  1303. * a new function skb_csum_dev() in net/core/skbuff.c).
  1304. */
  1305. dev->features = NETIF_F_HIGHDMA;
  1306. spin_lock_init(&data->txlock);
  1307. spin_lock_init(&data->misclock);
  1308. tsi108_reset_ether(data);
  1309. tsi108_kill_phy(dev);
  1310. if ((err = tsi108_get_mac(dev)) != 0) {
  1311. printk(KERN_ERR "%s: Invalid MAC address. Please correct.\n",
  1312. dev->name);
  1313. goto register_fail;
  1314. }
  1315. tsi108_init_mac(dev);
  1316. err = register_netdev(dev);
  1317. if (err) {
  1318. printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
  1319. dev->name);
  1320. goto register_fail;
  1321. }
  1322. platform_set_drvdata(pdev, dev);
  1323. printk(KERN_INFO "%s: Tsi108 Gigabit Ethernet, MAC: %pM\n",
  1324. dev->name, dev->dev_addr);
  1325. #ifdef DEBUG
  1326. data->msg_enable = DEBUG;
  1327. dump_eth_one(dev);
  1328. #endif
  1329. return 0;
  1330. register_fail:
  1331. iounmap(data->regs);
  1332. iounmap(data->phyregs);
  1333. regs_fail:
  1334. free_netdev(dev);
  1335. return err;
  1336. }
  1337. /* There's no way to either get interrupts from the PHY when
  1338. * something changes, or to have the Tsi108 automatically communicate
  1339. * with the PHY to reconfigure itself.
  1340. *
  1341. * Thus, we have to do it using a timer.
  1342. */
  1343. static void tsi108_timed_checker(unsigned long dev_ptr)
  1344. {
  1345. struct net_device *dev = (struct net_device *)dev_ptr;
  1346. struct tsi108_prv_data *data = netdev_priv(dev);
  1347. tsi108_check_phy(dev);
  1348. tsi108_check_rxring(dev);
  1349. mod_timer(&data->timer, jiffies + CHECK_PHY_INTERVAL);
  1350. }
  1351. static int tsi108_ether_init(void)
  1352. {
  1353. int ret;
  1354. ret = platform_driver_register (&tsi_eth_driver);
  1355. if (ret < 0){
  1356. printk("tsi108_ether_init: error initializing ethernet "
  1357. "device\n");
  1358. return ret;
  1359. }
  1360. return 0;
  1361. }
  1362. static int tsi108_ether_remove(struct platform_device *pdev)
  1363. {
  1364. struct net_device *dev = platform_get_drvdata(pdev);
  1365. struct tsi108_prv_data *priv = netdev_priv(dev);
  1366. unregister_netdev(dev);
  1367. tsi108_stop_ethernet(dev);
  1368. platform_set_drvdata(pdev, NULL);
  1369. iounmap(priv->regs);
  1370. iounmap(priv->phyregs);
  1371. free_netdev(dev);
  1372. return 0;
  1373. }
  1374. static void tsi108_ether_exit(void)
  1375. {
  1376. platform_driver_unregister(&tsi_eth_driver);
  1377. }
  1378. module_init(tsi108_ether_init);
  1379. module_exit(tsi108_ether_exit);
  1380. MODULE_AUTHOR("Tundra Semiconductor Corporation");
  1381. MODULE_DESCRIPTION("Tsi108 Gigabit Ethernet driver");
  1382. MODULE_LICENSE("GPL");
  1383. MODULE_ALIAS("platform:tsi-ethernet");