ks8851.c 40 KB

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