ks8851.c 39 KB

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