ks8851.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /* drivers/net/ks8851.c
  2. *
  3. * Copyright 2009 Simtec Electronics
  4. * http://www.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #define DEBUG
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/cache.h>
  20. #include <linux/crc32.h>
  21. #include <linux/mii.h>
  22. #include <linux/eeprom_93cx6.h>
  23. #include <linux/spi/spi.h>
  24. #include "ks8851.h"
  25. /**
  26. * struct ks8851_rxctrl - KS8851 driver rx control
  27. * @mchash: Multicast hash-table data.
  28. * @rxcr1: KS_RXCR1 register setting
  29. * @rxcr2: KS_RXCR2 register setting
  30. *
  31. * Representation of the settings needs to control the receive filtering
  32. * such as the multicast hash-filter and the receive register settings. This
  33. * is used to make the job of working out if the receive settings change and
  34. * then issuing the new settings to the worker that will send the necessary
  35. * commands.
  36. */
  37. struct ks8851_rxctrl {
  38. u16 mchash[4];
  39. u16 rxcr1;
  40. u16 rxcr2;
  41. };
  42. /**
  43. * union ks8851_tx_hdr - tx header data
  44. * @txb: The header as bytes
  45. * @txw: The header as 16bit, little-endian words
  46. *
  47. * A dual representation of the tx header data to allow
  48. * access to individual bytes, and to allow 16bit accesses
  49. * with 16bit alignment.
  50. */
  51. union ks8851_tx_hdr {
  52. u8 txb[6];
  53. __le16 txw[3];
  54. };
  55. /**
  56. * struct ks8851_net - KS8851 driver private data
  57. * @netdev: The network device we're bound to
  58. * @spidev: The spi device we're bound to.
  59. * @lock: Lock to ensure that the device is not accessed when busy.
  60. * @statelock: Lock on this structure for tx list.
  61. * @mii: The MII state information for the mii calls.
  62. * @rxctrl: RX settings for @rxctrl_work.
  63. * @tx_work: Work queue for tx packets
  64. * @irq_work: Work queue for servicing interrupts
  65. * @rxctrl_work: Work queue for updating RX mode and multicast lists
  66. * @txq: Queue of packets for transmission.
  67. * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
  68. * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
  69. * @txh: Space for generating packet TX header in DMA-able data
  70. * @rxd: Space for receiving SPI data, in DMA-able space.
  71. * @txd: Space for transmitting SPI data, in DMA-able space.
  72. * @msg_enable: The message flags controlling driver output (see ethtool).
  73. * @fid: Incrementing frame id tag.
  74. * @rc_ier: Cached copy of KS_IER.
  75. * @rc_ccr: Cached copy of KS_CCR.
  76. * @rc_rxqcr: Cached copy of KS_RXQCR.
  77. * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
  78. * @eeprom: 93CX6 EEPROM state for accessing on-board EEPROM.
  79. *
  80. * The @lock ensures that the chip is protected when certain operations are
  81. * in progress. When the read or write packet transfer is in progress, most
  82. * of the chip registers are not ccessible until the transfer is finished and
  83. * the DMA has been de-asserted.
  84. *
  85. * The @statelock is used to protect information in the structure which may
  86. * need to be accessed via several sources, such as the network driver layer
  87. * or one of the work queues.
  88. *
  89. * We align the buffers we may use for rx/tx to ensure that if the SPI driver
  90. * wants to DMA map them, it will not have any problems with data the driver
  91. * modifies.
  92. */
  93. struct ks8851_net {
  94. struct net_device *netdev;
  95. struct spi_device *spidev;
  96. struct mutex lock;
  97. spinlock_t statelock;
  98. union ks8851_tx_hdr txh ____cacheline_aligned;
  99. u8 rxd[8];
  100. u8 txd[8];
  101. u32 msg_enable ____cacheline_aligned;
  102. u16 tx_space;
  103. u8 fid;
  104. u16 rc_ier;
  105. u16 rc_rxqcr;
  106. u16 rc_ccr;
  107. u16 eeprom_size;
  108. struct mii_if_info mii;
  109. struct ks8851_rxctrl rxctrl;
  110. struct work_struct tx_work;
  111. struct work_struct irq_work;
  112. struct work_struct rxctrl_work;
  113. struct sk_buff_head txq;
  114. struct spi_message spi_msg1;
  115. struct spi_message spi_msg2;
  116. struct spi_transfer spi_xfer1;
  117. struct spi_transfer spi_xfer2[2];
  118. struct eeprom_93cx6 eeprom;
  119. };
  120. static int msg_enable;
  121. /* shift for byte-enable data */
  122. #define BYTE_EN(_x) ((_x) << 2)
  123. /* turn register number and byte-enable mask into data for start of packet */
  124. #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
  125. /* SPI register read/write calls.
  126. *
  127. * All these calls issue SPI transactions to access the chip's registers. They
  128. * all require that the necessary lock is held to prevent accesses when the
  129. * chip is busy transferring packet data (RX/TX FIFO accesses).
  130. */
  131. /**
  132. * ks8851_wrreg16 - write 16bit register value to chip
  133. * @ks: The chip state
  134. * @reg: The register address
  135. * @val: The value to write
  136. *
  137. * Issue a write to put the value @val into the register specified in @reg.
  138. */
  139. static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
  140. {
  141. struct spi_transfer *xfer = &ks->spi_xfer1;
  142. struct spi_message *msg = &ks->spi_msg1;
  143. __le16 txb[2];
  144. int ret;
  145. txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
  146. txb[1] = cpu_to_le16(val);
  147. xfer->tx_buf = txb;
  148. xfer->rx_buf = NULL;
  149. xfer->len = 4;
  150. ret = spi_sync(ks->spidev, msg);
  151. if (ret < 0)
  152. netdev_err(ks->netdev, "spi_sync() failed\n");
  153. }
  154. /**
  155. * ks8851_wrreg8 - write 8bit register value to chip
  156. * @ks: The chip state
  157. * @reg: The register address
  158. * @val: The value to write
  159. *
  160. * Issue a write to put the value @val into the register specified in @reg.
  161. */
  162. static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
  163. {
  164. struct spi_transfer *xfer = &ks->spi_xfer1;
  165. struct spi_message *msg = &ks->spi_msg1;
  166. __le16 txb[2];
  167. int ret;
  168. int bit;
  169. bit = 1 << (reg & 3);
  170. txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
  171. txb[1] = val;
  172. xfer->tx_buf = txb;
  173. xfer->rx_buf = NULL;
  174. xfer->len = 3;
  175. ret = spi_sync(ks->spidev, msg);
  176. if (ret < 0)
  177. netdev_err(ks->netdev, "spi_sync() failed\n");
  178. }
  179. /**
  180. * ks8851_rx_1msg - select whether to use one or two messages for spi read
  181. * @ks: The device structure
  182. *
  183. * Return whether to generate a single message with a tx and rx buffer
  184. * supplied to spi_sync(), or alternatively send the tx and rx buffers
  185. * as separate messages.
  186. *
  187. * Depending on the hardware in use, a single message may be more efficient
  188. * on interrupts or work done by the driver.
  189. *
  190. * This currently always returns true until we add some per-device data passed
  191. * from the platform code to specify which mode is better.
  192. */
  193. static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
  194. {
  195. return true;
  196. }
  197. /**
  198. * ks8851_rdreg - issue read register command and return the data
  199. * @ks: The device state
  200. * @op: The register address and byte enables in message format.
  201. * @rxb: The RX buffer to return the result into
  202. * @rxl: The length of data expected.
  203. *
  204. * This is the low level read call that issues the necessary spi message(s)
  205. * to read data from the register specified in @op.
  206. */
  207. static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
  208. u8 *rxb, unsigned rxl)
  209. {
  210. struct spi_transfer *xfer;
  211. struct spi_message *msg;
  212. __le16 *txb = (__le16 *)ks->txd;
  213. u8 *trx = ks->rxd;
  214. int ret;
  215. txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
  216. if (ks8851_rx_1msg(ks)) {
  217. msg = &ks->spi_msg1;
  218. xfer = &ks->spi_xfer1;
  219. xfer->tx_buf = txb;
  220. xfer->rx_buf = trx;
  221. xfer->len = rxl + 2;
  222. } else {
  223. msg = &ks->spi_msg2;
  224. xfer = ks->spi_xfer2;
  225. xfer->tx_buf = txb;
  226. xfer->rx_buf = NULL;
  227. xfer->len = 2;
  228. xfer++;
  229. xfer->tx_buf = NULL;
  230. xfer->rx_buf = trx;
  231. xfer->len = rxl;
  232. }
  233. ret = spi_sync(ks->spidev, msg);
  234. if (ret < 0)
  235. netdev_err(ks->netdev, "read: spi_sync() failed\n");
  236. else if (ks8851_rx_1msg(ks))
  237. memcpy(rxb, trx + 2, rxl);
  238. else
  239. memcpy(rxb, trx, rxl);
  240. }
  241. /**
  242. * ks8851_rdreg8 - read 8 bit register from device
  243. * @ks: The chip information
  244. * @reg: The register address
  245. *
  246. * Read a 8bit register from the chip, returning the result
  247. */
  248. static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
  249. {
  250. u8 rxb[1];
  251. ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
  252. return rxb[0];
  253. }
  254. /**
  255. * ks8851_rdreg16 - read 16 bit register from device
  256. * @ks: The chip information
  257. * @reg: The register address
  258. *
  259. * Read a 16bit register from the chip, returning the result
  260. */
  261. static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
  262. {
  263. __le16 rx = 0;
  264. ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
  265. return le16_to_cpu(rx);
  266. }
  267. /**
  268. * ks8851_rdreg32 - read 32 bit register from device
  269. * @ks: The chip information
  270. * @reg: The register address
  271. *
  272. * Read a 32bit register from the chip.
  273. *
  274. * Note, this read requires the address be aligned to 4 bytes.
  275. */
  276. static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
  277. {
  278. __le32 rx = 0;
  279. WARN_ON(reg & 3);
  280. ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
  281. return le32_to_cpu(rx);
  282. }
  283. /**
  284. * ks8851_soft_reset - issue one of the soft reset to the device
  285. * @ks: The device state.
  286. * @op: The bit(s) to set in the GRR
  287. *
  288. * Issue the relevant soft-reset command to the device's GRR register
  289. * specified by @op.
  290. *
  291. * Note, the delays are in there as a caution to ensure that the reset
  292. * has time to take effect and then complete. Since the datasheet does
  293. * not currently specify the exact sequence, we have chosen something
  294. * that seems to work with our device.
  295. */
  296. static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
  297. {
  298. ks8851_wrreg16(ks, KS_GRR, op);
  299. mdelay(1); /* wait a short time to effect reset */
  300. ks8851_wrreg16(ks, KS_GRR, 0);
  301. mdelay(1); /* wait for condition to clear */
  302. }
  303. /**
  304. * ks8851_set_powermode - set power mode of the device
  305. * @ks: The device state
  306. * @pwrmode: The power mode value to write to KS_PMECR.
  307. *
  308. * Change the power mode of the chip.
  309. */
  310. static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
  311. {
  312. unsigned pmecr;
  313. netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
  314. pmecr = ks8851_rdreg16(ks, KS_PMECR);
  315. pmecr &= ~PMECR_PM_MASK;
  316. pmecr |= pwrmode;
  317. ks8851_wrreg16(ks, KS_PMECR, pmecr);
  318. }
  319. /**
  320. * ks8851_write_mac_addr - write mac address to device registers
  321. * @dev: The network device
  322. *
  323. * Update the KS8851 MAC address registers from the address in @dev.
  324. *
  325. * This call assumes that the chip is not running, so there is no need to
  326. * shutdown the RXQ process whilst setting this.
  327. */
  328. static int ks8851_write_mac_addr(struct net_device *dev)
  329. {
  330. struct ks8851_net *ks = netdev_priv(dev);
  331. int i;
  332. mutex_lock(&ks->lock);
  333. /*
  334. * Wake up chip in case it was powered off when stopped; otherwise,
  335. * the first write to the MAC address does not take effect.
  336. */
  337. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  338. for (i = 0; i < ETH_ALEN; i++)
  339. ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
  340. if (!netif_running(dev))
  341. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  342. mutex_unlock(&ks->lock);
  343. return 0;
  344. }
  345. /**
  346. * ks8851_read_mac_addr - read mac address from device registers
  347. * @dev: The network device
  348. *
  349. * Update our copy of the KS8851 MAC address from the registers of @dev.
  350. */
  351. static void ks8851_read_mac_addr(struct net_device *dev)
  352. {
  353. struct ks8851_net *ks = netdev_priv(dev);
  354. int i;
  355. mutex_lock(&ks->lock);
  356. for (i = 0; i < ETH_ALEN; i++)
  357. dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
  358. mutex_unlock(&ks->lock);
  359. }
  360. /**
  361. * ks8851_init_mac - initialise the mac address
  362. * @ks: The device structure
  363. *
  364. * Get or create the initial mac address for the device and then set that
  365. * into the station address register. If there is an EEPROM present, then
  366. * we try that. If no valid mac address is found we use random_ether_addr()
  367. * to create a new one.
  368. */
  369. static void ks8851_init_mac(struct ks8851_net *ks)
  370. {
  371. struct net_device *dev = ks->netdev;
  372. /* first, try reading what we've got already */
  373. if (ks->rc_ccr & CCR_EEPROM) {
  374. ks8851_read_mac_addr(dev);
  375. if (is_valid_ether_addr(dev->dev_addr))
  376. return;
  377. netdev_err(ks->netdev, "invalid mac address read %pM\n",
  378. dev->dev_addr);
  379. }
  380. random_ether_addr(dev->dev_addr);
  381. ks8851_write_mac_addr(dev);
  382. }
  383. /**
  384. * ks8851_irq - device interrupt handler
  385. * @irq: Interrupt number passed from the IRQ hnalder.
  386. * @pw: The private word passed to register_irq(), our struct ks8851_net.
  387. *
  388. * Disable the interrupt from happening again until we've processed the
  389. * current status by scheduling ks8851_irq_work().
  390. */
  391. static irqreturn_t ks8851_irq(int irq, void *pw)
  392. {
  393. struct ks8851_net *ks = pw;
  394. disable_irq_nosync(irq);
  395. schedule_work(&ks->irq_work);
  396. return IRQ_HANDLED;
  397. }
  398. /**
  399. * ks8851_rdfifo - read data from the receive fifo
  400. * @ks: The device state.
  401. * @buff: The buffer address
  402. * @len: The length of the data to read
  403. *
  404. * Issue an RXQ FIFO read command and read the @len amount of data from
  405. * the FIFO into the buffer specified by @buff.
  406. */
  407. static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
  408. {
  409. struct spi_transfer *xfer = ks->spi_xfer2;
  410. struct spi_message *msg = &ks->spi_msg2;
  411. u8 txb[1];
  412. int ret;
  413. netif_dbg(ks, rx_status, ks->netdev,
  414. "%s: %d@%p\n", __func__, len, buff);
  415. /* set the operation we're issuing */
  416. txb[0] = KS_SPIOP_RXFIFO;
  417. xfer->tx_buf = txb;
  418. xfer->rx_buf = NULL;
  419. xfer->len = 1;
  420. xfer++;
  421. xfer->rx_buf = buff;
  422. xfer->tx_buf = NULL;
  423. xfer->len = len;
  424. ret = spi_sync(ks->spidev, msg);
  425. if (ret < 0)
  426. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  427. }
  428. /**
  429. * ks8851_dbg_dumpkkt - dump initial packet contents to debug
  430. * @ks: The device state
  431. * @rxpkt: The data for the received packet
  432. *
  433. * Dump the initial data from the packet to dev_dbg().
  434. */
  435. static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
  436. {
  437. netdev_dbg(ks->netdev,
  438. "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
  439. rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
  440. rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
  441. rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
  442. }
  443. /**
  444. * ks8851_rx_pkts - receive packets from the host
  445. * @ks: The device information.
  446. *
  447. * This is called from the IRQ work queue when the system detects that there
  448. * are packets in the receive queue. Find out how many packets there are and
  449. * read them from the FIFO.
  450. */
  451. static void ks8851_rx_pkts(struct ks8851_net *ks)
  452. {
  453. struct sk_buff *skb;
  454. unsigned rxfc;
  455. unsigned rxlen;
  456. unsigned rxstat;
  457. u32 rxh;
  458. u8 *rxpkt;
  459. rxfc = ks8851_rdreg8(ks, KS_RXFC);
  460. netif_dbg(ks, rx_status, ks->netdev,
  461. "%s: %d packets\n", __func__, rxfc);
  462. /* Currently we're issuing a read per packet, but we could possibly
  463. * improve the code by issuing a single read, getting the receive
  464. * header, allocating the packet and then reading the packet data
  465. * out in one go.
  466. *
  467. * This form of operation would require us to hold the SPI bus'
  468. * chipselect low during the entie transaction to avoid any
  469. * reset to the data stream coming from the chip.
  470. */
  471. for (; rxfc != 0; rxfc--) {
  472. rxh = ks8851_rdreg32(ks, KS_RXFHSR);
  473. rxstat = rxh & 0xffff;
  474. rxlen = rxh >> 16;
  475. netif_dbg(ks, rx_status, ks->netdev,
  476. "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
  477. /* the length of the packet includes the 32bit CRC */
  478. /* set dma read address */
  479. ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
  480. /* start the packet dma process, and set auto-dequeue rx */
  481. ks8851_wrreg16(ks, KS_RXQCR,
  482. ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
  483. if (rxlen > 4) {
  484. unsigned int rxalign;
  485. rxlen -= 4;
  486. rxalign = ALIGN(rxlen, 4);
  487. skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
  488. if (skb) {
  489. /* 4 bytes of status header + 4 bytes of
  490. * garbage: we put them before ethernet
  491. * header, so that they are copied,
  492. * but ignored.
  493. */
  494. rxpkt = skb_put(skb, rxlen) - 8;
  495. ks8851_rdfifo(ks, rxpkt, rxalign + 8);
  496. if (netif_msg_pktdata(ks))
  497. ks8851_dbg_dumpkkt(ks, rxpkt);
  498. skb->protocol = eth_type_trans(skb, ks->netdev);
  499. netif_rx(skb);
  500. ks->netdev->stats.rx_packets++;
  501. ks->netdev->stats.rx_bytes += rxlen;
  502. }
  503. }
  504. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  505. }
  506. }
  507. /**
  508. * ks8851_irq_work - work queue handler for dealing with interrupt requests
  509. * @work: The work structure that was scheduled by schedule_work()
  510. *
  511. * This is the handler invoked when the ks8851_irq() is called to find out
  512. * what happened, as we cannot allow ourselves to sleep whilst waiting for
  513. * anything other process has the chip's lock.
  514. *
  515. * Read the interrupt status, work out what needs to be done and then clear
  516. * any of the interrupts that are not needed.
  517. */
  518. static void ks8851_irq_work(struct work_struct *work)
  519. {
  520. struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
  521. unsigned status;
  522. unsigned handled = 0;
  523. mutex_lock(&ks->lock);
  524. status = ks8851_rdreg16(ks, KS_ISR);
  525. netif_dbg(ks, intr, ks->netdev,
  526. "%s: status 0x%04x\n", __func__, status);
  527. if (status & IRQ_LCI) {
  528. /* should do something about checking link status */
  529. handled |= IRQ_LCI;
  530. }
  531. if (status & IRQ_LDI) {
  532. u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
  533. pmecr &= ~PMECR_WKEVT_MASK;
  534. ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
  535. handled |= IRQ_LDI;
  536. }
  537. if (status & IRQ_RXPSI)
  538. handled |= IRQ_RXPSI;
  539. if (status & IRQ_TXI) {
  540. handled |= IRQ_TXI;
  541. /* no lock here, tx queue should have been stopped */
  542. /* update our idea of how much tx space is available to the
  543. * system */
  544. ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
  545. netif_dbg(ks, intr, ks->netdev,
  546. "%s: txspace %d\n", __func__, ks->tx_space);
  547. }
  548. if (status & IRQ_RXI)
  549. handled |= IRQ_RXI;
  550. if (status & IRQ_SPIBEI) {
  551. dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
  552. handled |= IRQ_SPIBEI;
  553. }
  554. ks8851_wrreg16(ks, KS_ISR, handled);
  555. if (status & IRQ_RXI) {
  556. /* the datasheet says to disable the rx interrupt during
  557. * packet read-out, however we're masking the interrupt
  558. * from the device so do not bother masking just the RX
  559. * from the device. */
  560. ks8851_rx_pkts(ks);
  561. }
  562. /* if something stopped the rx process, probably due to wanting
  563. * to change the rx settings, then do something about restarting
  564. * it. */
  565. if (status & IRQ_RXPSI) {
  566. struct ks8851_rxctrl *rxc = &ks->rxctrl;
  567. /* update the multicast hash table */
  568. ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
  569. ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
  570. ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
  571. ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
  572. ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
  573. ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
  574. }
  575. mutex_unlock(&ks->lock);
  576. if (status & IRQ_TXI)
  577. netif_wake_queue(ks->netdev);
  578. enable_irq(ks->netdev->irq);
  579. }
  580. /**
  581. * calc_txlen - calculate size of message to send packet
  582. * @len: Length of data
  583. *
  584. * Returns the size of the TXFIFO message needed to send
  585. * this packet.
  586. */
  587. static inline unsigned calc_txlen(unsigned len)
  588. {
  589. return ALIGN(len + 4, 4);
  590. }
  591. /**
  592. * ks8851_wrpkt - write packet to TX FIFO
  593. * @ks: The device state.
  594. * @txp: The sk_buff to transmit.
  595. * @irq: IRQ on completion of the packet.
  596. *
  597. * Send the @txp to the chip. This means creating the relevant packet header
  598. * specifying the length of the packet and the other information the chip
  599. * needs, such as IRQ on completion. Send the header and the packet data to
  600. * the device.
  601. */
  602. static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
  603. {
  604. struct spi_transfer *xfer = ks->spi_xfer2;
  605. struct spi_message *msg = &ks->spi_msg2;
  606. unsigned fid = 0;
  607. int ret;
  608. netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
  609. __func__, txp, txp->len, txp->data, irq);
  610. fid = ks->fid++;
  611. fid &= TXFR_TXFID_MASK;
  612. if (irq)
  613. fid |= TXFR_TXIC; /* irq on completion */
  614. /* start header at txb[1] to align txw entries */
  615. ks->txh.txb[1] = KS_SPIOP_TXFIFO;
  616. ks->txh.txw[1] = cpu_to_le16(fid);
  617. ks->txh.txw[2] = cpu_to_le16(txp->len);
  618. xfer->tx_buf = &ks->txh.txb[1];
  619. xfer->rx_buf = NULL;
  620. xfer->len = 5;
  621. xfer++;
  622. xfer->tx_buf = txp->data;
  623. xfer->rx_buf = NULL;
  624. xfer->len = ALIGN(txp->len, 4);
  625. ret = spi_sync(ks->spidev, msg);
  626. if (ret < 0)
  627. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  628. }
  629. /**
  630. * ks8851_done_tx - update and then free skbuff after transmitting
  631. * @ks: The device state
  632. * @txb: The buffer transmitted
  633. */
  634. static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
  635. {
  636. struct net_device *dev = ks->netdev;
  637. dev->stats.tx_bytes += txb->len;
  638. dev->stats.tx_packets++;
  639. dev_kfree_skb(txb);
  640. }
  641. /**
  642. * ks8851_tx_work - process tx packet(s)
  643. * @work: The work strucutre what was scheduled.
  644. *
  645. * This is called when a number of packets have been scheduled for
  646. * transmission and need to be sent to the device.
  647. */
  648. static void ks8851_tx_work(struct work_struct *work)
  649. {
  650. struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
  651. struct sk_buff *txb;
  652. bool last = skb_queue_empty(&ks->txq);
  653. mutex_lock(&ks->lock);
  654. while (!last) {
  655. txb = skb_dequeue(&ks->txq);
  656. last = skb_queue_empty(&ks->txq);
  657. if (txb != NULL) {
  658. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  659. ks8851_wrpkt(ks, txb, last);
  660. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  661. ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
  662. ks8851_done_tx(ks, txb);
  663. }
  664. }
  665. mutex_unlock(&ks->lock);
  666. }
  667. /**
  668. * ks8851_net_open - open network device
  669. * @dev: The network device being opened.
  670. *
  671. * Called when the network device is marked active, such as a user executing
  672. * 'ifconfig up' on the device.
  673. */
  674. static int ks8851_net_open(struct net_device *dev)
  675. {
  676. struct ks8851_net *ks = netdev_priv(dev);
  677. /* lock the card, even if we may not actually be doing anything
  678. * else at the moment */
  679. mutex_lock(&ks->lock);
  680. netif_dbg(ks, ifup, ks->netdev, "opening\n");
  681. /* bring chip out of any power saving mode it was in */
  682. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  683. /* issue a soft reset to the RX/TX QMU to put it into a known
  684. * state. */
  685. ks8851_soft_reset(ks, GRR_QMU);
  686. /* setup transmission parameters */
  687. ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
  688. TXCR_TXPE | /* pad to min length */
  689. TXCR_TXCRC | /* add CRC */
  690. TXCR_TXFCE)); /* enable flow control */
  691. /* auto-increment tx data, reset tx pointer */
  692. ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
  693. /* setup receiver control */
  694. ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
  695. RXCR1_RXFCE | /* enable flow control */
  696. RXCR1_RXBE | /* broadcast enable */
  697. RXCR1_RXUE | /* unicast enable */
  698. RXCR1_RXE)); /* enable rx block */
  699. /* transfer entire frames out in one go */
  700. ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
  701. /* set receive counter timeouts */
  702. ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
  703. ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
  704. ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
  705. ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
  706. RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
  707. RXQCR_RXDTTE); /* IRQ on time exceeded */
  708. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  709. /* clear then enable interrupts */
  710. #define STD_IRQ (IRQ_LCI | /* Link Change */ \
  711. IRQ_TXI | /* TX done */ \
  712. IRQ_RXI | /* RX done */ \
  713. IRQ_SPIBEI | /* SPI bus error */ \
  714. IRQ_TXPSI | /* TX process stop */ \
  715. IRQ_RXPSI) /* RX process stop */
  716. ks->rc_ier = STD_IRQ;
  717. ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
  718. ks8851_wrreg16(ks, KS_IER, STD_IRQ);
  719. netif_start_queue(ks->netdev);
  720. netif_dbg(ks, ifup, ks->netdev, "network device up\n");
  721. mutex_unlock(&ks->lock);
  722. return 0;
  723. }
  724. /**
  725. * ks8851_net_stop - close network device
  726. * @dev: The device being closed.
  727. *
  728. * Called to close down a network device which has been active. Cancell any
  729. * work, shutdown the RX and TX process and then place the chip into a low
  730. * power state whilst it is not being used.
  731. */
  732. static int ks8851_net_stop(struct net_device *dev)
  733. {
  734. struct ks8851_net *ks = netdev_priv(dev);
  735. netif_info(ks, ifdown, dev, "shutting down\n");
  736. netif_stop_queue(dev);
  737. mutex_lock(&ks->lock);
  738. /* stop any outstanding work */
  739. flush_work(&ks->irq_work);
  740. flush_work(&ks->tx_work);
  741. flush_work(&ks->rxctrl_work);
  742. /* turn off the IRQs and ack any outstanding */
  743. ks8851_wrreg16(ks, KS_IER, 0x0000);
  744. ks8851_wrreg16(ks, KS_ISR, 0xffff);
  745. /* shutdown RX process */
  746. ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
  747. /* shutdown TX process */
  748. ks8851_wrreg16(ks, KS_TXCR, 0x0000);
  749. /* set powermode to soft power down to save power */
  750. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  751. /* ensure any queued tx buffers are dumped */
  752. while (!skb_queue_empty(&ks->txq)) {
  753. struct sk_buff *txb = skb_dequeue(&ks->txq);
  754. netif_dbg(ks, ifdown, ks->netdev,
  755. "%s: freeing txb %p\n", __func__, txb);
  756. dev_kfree_skb(txb);
  757. }
  758. mutex_unlock(&ks->lock);
  759. return 0;
  760. }
  761. /**
  762. * ks8851_start_xmit - transmit packet
  763. * @skb: The buffer to transmit
  764. * @dev: The device used to transmit the packet.
  765. *
  766. * Called by the network layer to transmit the @skb. Queue the packet for
  767. * the device and schedule the necessary work to transmit the packet when
  768. * it is free.
  769. *
  770. * We do this to firstly avoid sleeping with the network device locked,
  771. * and secondly so we can round up more than one packet to transmit which
  772. * means we can try and avoid generating too many transmit done interrupts.
  773. */
  774. static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
  775. struct net_device *dev)
  776. {
  777. struct ks8851_net *ks = netdev_priv(dev);
  778. unsigned needed = calc_txlen(skb->len);
  779. netdev_tx_t ret = NETDEV_TX_OK;
  780. netif_dbg(ks, tx_queued, ks->netdev,
  781. "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
  782. spin_lock(&ks->statelock);
  783. if (needed > ks->tx_space) {
  784. netif_stop_queue(dev);
  785. ret = NETDEV_TX_BUSY;
  786. } else {
  787. ks->tx_space -= needed;
  788. skb_queue_tail(&ks->txq, skb);
  789. }
  790. spin_unlock(&ks->statelock);
  791. schedule_work(&ks->tx_work);
  792. return ret;
  793. }
  794. /**
  795. * ks8851_rxctrl_work - work handler to change rx mode
  796. * @work: The work structure this belongs to.
  797. *
  798. * Lock the device and issue the necessary changes to the receive mode from
  799. * the network device layer. This is done so that we can do this without
  800. * having to sleep whilst holding the network device lock.
  801. *
  802. * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
  803. * receive parameters are programmed, we issue a write to disable the RXQ and
  804. * then wait for the interrupt handler to be triggered once the RXQ shutdown is
  805. * complete. The interrupt handler then writes the new values into the chip.
  806. */
  807. static void ks8851_rxctrl_work(struct work_struct *work)
  808. {
  809. struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
  810. mutex_lock(&ks->lock);
  811. /* need to shutdown RXQ before modifying filter parameters */
  812. ks8851_wrreg16(ks, KS_RXCR1, 0x00);
  813. mutex_unlock(&ks->lock);
  814. }
  815. static void ks8851_set_rx_mode(struct net_device *dev)
  816. {
  817. struct ks8851_net *ks = netdev_priv(dev);
  818. struct ks8851_rxctrl rxctrl;
  819. memset(&rxctrl, 0, sizeof(rxctrl));
  820. if (dev->flags & IFF_PROMISC) {
  821. /* interface to receive everything */
  822. rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
  823. } else if (dev->flags & IFF_ALLMULTI) {
  824. /* accept all multicast packets */
  825. rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
  826. RXCR1_RXPAFMA | RXCR1_RXMAFMA);
  827. } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
  828. struct netdev_hw_addr *ha;
  829. u32 crc;
  830. /* accept some multicast */
  831. netdev_for_each_mc_addr(ha, dev) {
  832. crc = ether_crc(ETH_ALEN, ha->addr);
  833. crc >>= (32 - 6); /* get top six bits */
  834. rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
  835. }
  836. rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
  837. } else {
  838. /* just accept broadcast / unicast */
  839. rxctrl.rxcr1 = RXCR1_RXPAFMA;
  840. }
  841. rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
  842. RXCR1_RXBE | /* broadcast enable */
  843. RXCR1_RXE | /* RX process enable */
  844. RXCR1_RXFCE); /* enable flow control */
  845. rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
  846. /* schedule work to do the actual set of the data if needed */
  847. spin_lock(&ks->statelock);
  848. if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
  849. memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
  850. schedule_work(&ks->rxctrl_work);
  851. }
  852. spin_unlock(&ks->statelock);
  853. }
  854. static int ks8851_set_mac_address(struct net_device *dev, void *addr)
  855. {
  856. struct sockaddr *sa = addr;
  857. if (netif_running(dev))
  858. return -EBUSY;
  859. if (!is_valid_ether_addr(sa->sa_data))
  860. return -EADDRNOTAVAIL;
  861. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  862. return ks8851_write_mac_addr(dev);
  863. }
  864. static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  865. {
  866. struct ks8851_net *ks = netdev_priv(dev);
  867. if (!netif_running(dev))
  868. return -EINVAL;
  869. return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
  870. }
  871. static const struct net_device_ops ks8851_netdev_ops = {
  872. .ndo_open = ks8851_net_open,
  873. .ndo_stop = ks8851_net_stop,
  874. .ndo_do_ioctl = ks8851_net_ioctl,
  875. .ndo_start_xmit = ks8851_start_xmit,
  876. .ndo_set_mac_address = ks8851_set_mac_address,
  877. .ndo_set_rx_mode = ks8851_set_rx_mode,
  878. .ndo_change_mtu = eth_change_mtu,
  879. .ndo_validate_addr = eth_validate_addr,
  880. };
  881. /* ethtool support */
  882. static void ks8851_get_drvinfo(struct net_device *dev,
  883. struct ethtool_drvinfo *di)
  884. {
  885. strlcpy(di->driver, "KS8851", sizeof(di->driver));
  886. strlcpy(di->version, "1.00", sizeof(di->version));
  887. strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
  888. }
  889. static u32 ks8851_get_msglevel(struct net_device *dev)
  890. {
  891. struct ks8851_net *ks = netdev_priv(dev);
  892. return ks->msg_enable;
  893. }
  894. static void ks8851_set_msglevel(struct net_device *dev, u32 to)
  895. {
  896. struct ks8851_net *ks = netdev_priv(dev);
  897. ks->msg_enable = to;
  898. }
  899. static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  900. {
  901. struct ks8851_net *ks = netdev_priv(dev);
  902. return mii_ethtool_gset(&ks->mii, cmd);
  903. }
  904. static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  905. {
  906. struct ks8851_net *ks = netdev_priv(dev);
  907. return mii_ethtool_sset(&ks->mii, cmd);
  908. }
  909. static u32 ks8851_get_link(struct net_device *dev)
  910. {
  911. struct ks8851_net *ks = netdev_priv(dev);
  912. return mii_link_ok(&ks->mii);
  913. }
  914. static int ks8851_nway_reset(struct net_device *dev)
  915. {
  916. struct ks8851_net *ks = netdev_priv(dev);
  917. return mii_nway_restart(&ks->mii);
  918. }
  919. /* EEPROM support */
  920. static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee)
  921. {
  922. struct ks8851_net *ks = ee->data;
  923. unsigned val;
  924. val = ks8851_rdreg16(ks, KS_EEPCR);
  925. ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0;
  926. ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0;
  927. ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0;
  928. }
  929. static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
  930. {
  931. struct ks8851_net *ks = ee->data;
  932. unsigned val = EEPCR_EESA; /* default - eeprom access on */
  933. if (ee->drive_data)
  934. val |= EEPCR_EESRWA;
  935. if (ee->reg_data_in)
  936. val |= EEPCR_EEDO;
  937. if (ee->reg_data_clock)
  938. val |= EEPCR_EESCK;
  939. if (ee->reg_chip_select)
  940. val |= EEPCR_EECS;
  941. ks8851_wrreg16(ks, KS_EEPCR, val);
  942. }
  943. /**
  944. * ks8851_eeprom_claim - claim device EEPROM and activate the interface
  945. * @ks: The network device state.
  946. *
  947. * Check for the presence of an EEPROM, and then activate software access
  948. * to the device.
  949. */
  950. static int ks8851_eeprom_claim(struct ks8851_net *ks)
  951. {
  952. if (!(ks->rc_ccr & CCR_EEPROM))
  953. return -ENOENT;
  954. mutex_lock(&ks->lock);
  955. /* start with clock low, cs high */
  956. ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
  957. return 0;
  958. }
  959. /**
  960. * ks8851_eeprom_release - release the EEPROM interface
  961. * @ks: The device state
  962. *
  963. * Release the software access to the device EEPROM
  964. */
  965. static void ks8851_eeprom_release(struct ks8851_net *ks)
  966. {
  967. unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
  968. ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
  969. mutex_unlock(&ks->lock);
  970. }
  971. #define KS_EEPROM_MAGIC (0x00008851)
  972. static int ks8851_set_eeprom(struct net_device *dev,
  973. struct ethtool_eeprom *ee, u8 *data)
  974. {
  975. struct ks8851_net *ks = netdev_priv(dev);
  976. int offset = ee->offset;
  977. int len = ee->len;
  978. u16 tmp;
  979. /* currently only support byte writing */
  980. if (len != 1)
  981. return -EINVAL;
  982. if (ee->magic != KS_EEPROM_MAGIC)
  983. return -EINVAL;
  984. if (ks8851_eeprom_claim(ks))
  985. return -ENOENT;
  986. eeprom_93cx6_wren(&ks->eeprom, true);
  987. /* ethtool currently only supports writing bytes, which means
  988. * we have to read/modify/write our 16bit EEPROMs */
  989. eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp);
  990. if (offset & 1) {
  991. tmp &= 0xff;
  992. tmp |= *data << 8;
  993. } else {
  994. tmp &= 0xff00;
  995. tmp |= *data;
  996. }
  997. eeprom_93cx6_write(&ks->eeprom, offset/2, tmp);
  998. eeprom_93cx6_wren(&ks->eeprom, false);
  999. ks8851_eeprom_release(ks);
  1000. return 0;
  1001. }
  1002. static int ks8851_get_eeprom(struct net_device *dev,
  1003. struct ethtool_eeprom *ee, u8 *data)
  1004. {
  1005. struct ks8851_net *ks = netdev_priv(dev);
  1006. int offset = ee->offset;
  1007. int len = ee->len;
  1008. /* must be 2 byte aligned */
  1009. if (len & 1 || offset & 1)
  1010. return -EINVAL;
  1011. if (ks8851_eeprom_claim(ks))
  1012. return -ENOENT;
  1013. ee->magic = KS_EEPROM_MAGIC;
  1014. eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
  1015. ks8851_eeprom_release(ks);
  1016. return 0;
  1017. }
  1018. static int ks8851_get_eeprom_len(struct net_device *dev)
  1019. {
  1020. struct ks8851_net *ks = netdev_priv(dev);
  1021. /* currently, we assume it is an 93C46 attached, so return 128 */
  1022. return ks->rc_ccr & CCR_EEPROM ? 128 : 0;
  1023. }
  1024. static const struct ethtool_ops ks8851_ethtool_ops = {
  1025. .get_drvinfo = ks8851_get_drvinfo,
  1026. .get_msglevel = ks8851_get_msglevel,
  1027. .set_msglevel = ks8851_set_msglevel,
  1028. .get_settings = ks8851_get_settings,
  1029. .set_settings = ks8851_set_settings,
  1030. .get_link = ks8851_get_link,
  1031. .nway_reset = ks8851_nway_reset,
  1032. .get_eeprom_len = ks8851_get_eeprom_len,
  1033. .get_eeprom = ks8851_get_eeprom,
  1034. .set_eeprom = ks8851_set_eeprom,
  1035. };
  1036. /* MII interface controls */
  1037. /**
  1038. * ks8851_phy_reg - convert MII register into a KS8851 register
  1039. * @reg: MII register number.
  1040. *
  1041. * Return the KS8851 register number for the corresponding MII PHY register
  1042. * if possible. Return zero if the MII register has no direct mapping to the
  1043. * KS8851 register set.
  1044. */
  1045. static int ks8851_phy_reg(int reg)
  1046. {
  1047. switch (reg) {
  1048. case MII_BMCR:
  1049. return KS_P1MBCR;
  1050. case MII_BMSR:
  1051. return KS_P1MBSR;
  1052. case MII_PHYSID1:
  1053. return KS_PHY1ILR;
  1054. case MII_PHYSID2:
  1055. return KS_PHY1IHR;
  1056. case MII_ADVERTISE:
  1057. return KS_P1ANAR;
  1058. case MII_LPA:
  1059. return KS_P1ANLPR;
  1060. }
  1061. return 0x0;
  1062. }
  1063. /**
  1064. * ks8851_phy_read - MII interface PHY register read.
  1065. * @dev: The network device the PHY is on.
  1066. * @phy_addr: Address of PHY (ignored as we only have one)
  1067. * @reg: The register to read.
  1068. *
  1069. * This call reads data from the PHY register specified in @reg. Since the
  1070. * device does not support all the MII registers, the non-existent values
  1071. * are always returned as zero.
  1072. *
  1073. * We return zero for unsupported registers as the MII code does not check
  1074. * the value returned for any error status, and simply returns it to the
  1075. * caller. The mii-tool that the driver was tested with takes any -ve error
  1076. * as real PHY capabilities, thus displaying incorrect data to the user.
  1077. */
  1078. static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
  1079. {
  1080. struct ks8851_net *ks = netdev_priv(dev);
  1081. int ksreg;
  1082. int result;
  1083. ksreg = ks8851_phy_reg(reg);
  1084. if (!ksreg)
  1085. return 0x0; /* no error return allowed, so use zero */
  1086. mutex_lock(&ks->lock);
  1087. result = ks8851_rdreg16(ks, ksreg);
  1088. mutex_unlock(&ks->lock);
  1089. return result;
  1090. }
  1091. static void ks8851_phy_write(struct net_device *dev,
  1092. int phy, int reg, int value)
  1093. {
  1094. struct ks8851_net *ks = netdev_priv(dev);
  1095. int ksreg;
  1096. ksreg = ks8851_phy_reg(reg);
  1097. if (ksreg) {
  1098. mutex_lock(&ks->lock);
  1099. ks8851_wrreg16(ks, ksreg, value);
  1100. mutex_unlock(&ks->lock);
  1101. }
  1102. }
  1103. /**
  1104. * ks8851_read_selftest - read the selftest memory info.
  1105. * @ks: The device state
  1106. *
  1107. * Read and check the TX/RX memory selftest information.
  1108. */
  1109. static int ks8851_read_selftest(struct ks8851_net *ks)
  1110. {
  1111. unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
  1112. int ret = 0;
  1113. unsigned rd;
  1114. rd = ks8851_rdreg16(ks, KS_MBIR);
  1115. if ((rd & both_done) != both_done) {
  1116. netdev_warn(ks->netdev, "Memory selftest not finished\n");
  1117. return 0;
  1118. }
  1119. if (rd & MBIR_TXMBFA) {
  1120. netdev_err(ks->netdev, "TX memory selftest fail\n");
  1121. ret |= 1;
  1122. }
  1123. if (rd & MBIR_RXMBFA) {
  1124. netdev_err(ks->netdev, "RX memory selftest fail\n");
  1125. ret |= 2;
  1126. }
  1127. return 0;
  1128. }
  1129. /* driver bus management functions */
  1130. #ifdef CONFIG_PM
  1131. static int ks8851_suspend(struct spi_device *spi, pm_message_t state)
  1132. {
  1133. struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
  1134. struct net_device *dev = ks->netdev;
  1135. if (netif_running(dev)) {
  1136. netif_device_detach(dev);
  1137. ks8851_net_stop(dev);
  1138. }
  1139. return 0;
  1140. }
  1141. static int ks8851_resume(struct spi_device *spi)
  1142. {
  1143. struct ks8851_net *ks = dev_get_drvdata(&spi->dev);
  1144. struct net_device *dev = ks->netdev;
  1145. if (netif_running(dev)) {
  1146. ks8851_net_open(dev);
  1147. netif_device_attach(dev);
  1148. }
  1149. return 0;
  1150. }
  1151. #else
  1152. #define ks8851_suspend NULL
  1153. #define ks8851_resume NULL
  1154. #endif
  1155. static int __devinit ks8851_probe(struct spi_device *spi)
  1156. {
  1157. struct net_device *ndev;
  1158. struct ks8851_net *ks;
  1159. int ret;
  1160. ndev = alloc_etherdev(sizeof(struct ks8851_net));
  1161. if (!ndev) {
  1162. dev_err(&spi->dev, "failed to alloc ethernet device\n");
  1163. return -ENOMEM;
  1164. }
  1165. spi->bits_per_word = 8;
  1166. ks = netdev_priv(ndev);
  1167. ks->netdev = ndev;
  1168. ks->spidev = spi;
  1169. ks->tx_space = 6144;
  1170. mutex_init(&ks->lock);
  1171. spin_lock_init(&ks->statelock);
  1172. INIT_WORK(&ks->tx_work, ks8851_tx_work);
  1173. INIT_WORK(&ks->irq_work, ks8851_irq_work);
  1174. INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
  1175. /* initialise pre-made spi transfer messages */
  1176. spi_message_init(&ks->spi_msg1);
  1177. spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
  1178. spi_message_init(&ks->spi_msg2);
  1179. spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
  1180. spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
  1181. /* setup EEPROM state */
  1182. ks->eeprom.data = ks;
  1183. ks->eeprom.width = PCI_EEPROM_WIDTH_93C46;
  1184. ks->eeprom.register_read = ks8851_eeprom_regread;
  1185. ks->eeprom.register_write = ks8851_eeprom_regwrite;
  1186. /* setup mii state */
  1187. ks->mii.dev = ndev;
  1188. ks->mii.phy_id = 1,
  1189. ks->mii.phy_id_mask = 1;
  1190. ks->mii.reg_num_mask = 0xf;
  1191. ks->mii.mdio_read = ks8851_phy_read;
  1192. ks->mii.mdio_write = ks8851_phy_write;
  1193. dev_info(&spi->dev, "message enable is %d\n", msg_enable);
  1194. /* set the default message enable */
  1195. ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
  1196. NETIF_MSG_PROBE |
  1197. NETIF_MSG_LINK));
  1198. skb_queue_head_init(&ks->txq);
  1199. SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
  1200. SET_NETDEV_DEV(ndev, &spi->dev);
  1201. dev_set_drvdata(&spi->dev, ks);
  1202. ndev->if_port = IF_PORT_100BASET;
  1203. ndev->netdev_ops = &ks8851_netdev_ops;
  1204. ndev->irq = spi->irq;
  1205. /* issue a global soft reset to reset the device. */
  1206. ks8851_soft_reset(ks, GRR_GSR);
  1207. /* simple check for a valid chip being connected to the bus */
  1208. if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
  1209. dev_err(&spi->dev, "failed to read device ID\n");
  1210. ret = -ENODEV;
  1211. goto err_id;
  1212. }
  1213. /* cache the contents of the CCR register for EEPROM, etc. */
  1214. ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
  1215. if (ks->rc_ccr & CCR_EEPROM)
  1216. ks->eeprom_size = 128;
  1217. else
  1218. ks->eeprom_size = 0;
  1219. ks8851_read_selftest(ks);
  1220. ks8851_init_mac(ks);
  1221. ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
  1222. ndev->name, ks);
  1223. if (ret < 0) {
  1224. dev_err(&spi->dev, "failed to get irq\n");
  1225. goto err_irq;
  1226. }
  1227. ret = register_netdev(ndev);
  1228. if (ret) {
  1229. dev_err(&spi->dev, "failed to register network device\n");
  1230. goto err_netdev;
  1231. }
  1232. netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
  1233. CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
  1234. ndev->dev_addr, ndev->irq,
  1235. ks->rc_ccr & CCR_EEPROM ? "has" : "no");
  1236. return 0;
  1237. err_netdev:
  1238. free_irq(ndev->irq, ndev);
  1239. err_id:
  1240. err_irq:
  1241. free_netdev(ndev);
  1242. return ret;
  1243. }
  1244. static int __devexit ks8851_remove(struct spi_device *spi)
  1245. {
  1246. struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
  1247. if (netif_msg_drv(priv))
  1248. dev_info(&spi->dev, "remove\n");
  1249. unregister_netdev(priv->netdev);
  1250. free_irq(spi->irq, priv);
  1251. free_netdev(priv->netdev);
  1252. return 0;
  1253. }
  1254. static struct spi_driver ks8851_driver = {
  1255. .driver = {
  1256. .name = "ks8851",
  1257. .owner = THIS_MODULE,
  1258. },
  1259. .probe = ks8851_probe,
  1260. .remove = __devexit_p(ks8851_remove),
  1261. .suspend = ks8851_suspend,
  1262. .resume = ks8851_resume,
  1263. };
  1264. static int __init ks8851_init(void)
  1265. {
  1266. return spi_register_driver(&ks8851_driver);
  1267. }
  1268. static void __exit ks8851_exit(void)
  1269. {
  1270. spi_unregister_driver(&ks8851_driver);
  1271. }
  1272. module_init(ks8851_init);
  1273. module_exit(ks8851_exit);
  1274. MODULE_DESCRIPTION("KS8851 Network driver");
  1275. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1276. MODULE_LICENSE("GPL");
  1277. module_param_named(message, msg_enable, int, 0);
  1278. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
  1279. MODULE_ALIAS("spi:ks8851");