ks8851.c 40 KB

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