ks8851.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /* drivers/net/ethernet/micrel/ks8851.c
  2. *
  3. * Copyright 2009 Simtec Electronics
  4. * http://www.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #define DEBUG
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/cache.h>
  20. #include <linux/crc32.h>
  21. #include <linux/mii.h>
  22. #include <linux/eeprom_93cx6.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/gpio.h>
  26. #include <linux/of_gpio.h>
  27. #include "ks8851.h"
  28. /**
  29. * struct ks8851_rxctrl - KS8851 driver rx control
  30. * @mchash: Multicast hash-table data.
  31. * @rxcr1: KS_RXCR1 register setting
  32. * @rxcr2: KS_RXCR2 register setting
  33. *
  34. * Representation of the settings needs to control the receive filtering
  35. * such as the multicast hash-filter and the receive register settings. This
  36. * is used to make the job of working out if the receive settings change and
  37. * then issuing the new settings to the worker that will send the necessary
  38. * commands.
  39. */
  40. struct ks8851_rxctrl {
  41. u16 mchash[4];
  42. u16 rxcr1;
  43. u16 rxcr2;
  44. };
  45. /**
  46. * union ks8851_tx_hdr - tx header data
  47. * @txb: The header as bytes
  48. * @txw: The header as 16bit, little-endian words
  49. *
  50. * A dual representation of the tx header data to allow
  51. * access to individual bytes, and to allow 16bit accesses
  52. * with 16bit alignment.
  53. */
  54. union ks8851_tx_hdr {
  55. u8 txb[6];
  56. __le16 txw[3];
  57. };
  58. /**
  59. * struct ks8851_net - KS8851 driver private data
  60. * @netdev: The network device we're bound to
  61. * @spidev: The spi device we're bound to.
  62. * @lock: Lock to ensure that the device is not accessed when busy.
  63. * @statelock: Lock on this structure for tx list.
  64. * @mii: The MII state information for the mii calls.
  65. * @rxctrl: RX settings for @rxctrl_work.
  66. * @tx_work: Work queue for tx packets
  67. * @rxctrl_work: Work queue for updating RX mode and multicast lists
  68. * @txq: Queue of packets for transmission.
  69. * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1.
  70. * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2.
  71. * @txh: Space for generating packet TX header in DMA-able data
  72. * @rxd: Space for receiving SPI data, in DMA-able space.
  73. * @txd: Space for transmitting SPI data, in DMA-able space.
  74. * @msg_enable: The message flags controlling driver output (see ethtool).
  75. * @fid: Incrementing frame id tag.
  76. * @rc_ier: Cached copy of KS_IER.
  77. * @rc_ccr: Cached copy of KS_CCR.
  78. * @rc_rxqcr: Cached copy of KS_RXQCR.
  79. * @eeprom_size: Companion eeprom size in Bytes, 0 if no eeprom
  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. u16 eeprom_size;
  113. struct mii_if_info mii;
  114. struct ks8851_rxctrl rxctrl;
  115. struct work_struct tx_work;
  116. struct work_struct rxctrl_work;
  117. struct sk_buff_head txq;
  118. struct spi_message spi_msg1;
  119. struct spi_message spi_msg2;
  120. struct spi_transfer spi_xfer1;
  121. struct spi_transfer spi_xfer2[2];
  122. struct eeprom_93cx6 eeprom;
  123. struct regulator *vdd_reg;
  124. struct regulator *vdd_io;
  125. int gpio;
  126. };
  127. static int msg_enable;
  128. /* shift for byte-enable data */
  129. #define BYTE_EN(_x) ((_x) << 2)
  130. /* turn register number and byte-enable mask into data for start of packet */
  131. #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
  132. /* SPI register read/write calls.
  133. *
  134. * All these calls issue SPI transactions to access the chip's registers. They
  135. * all require that the necessary lock is held to prevent accesses when the
  136. * chip is busy transferring packet data (RX/TX FIFO accesses).
  137. */
  138. /**
  139. * ks8851_wrreg16 - write 16bit register value to chip
  140. * @ks: The chip state
  141. * @reg: The register address
  142. * @val: The value to write
  143. *
  144. * Issue a write to put the value @val into the register specified in @reg.
  145. */
  146. static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
  147. {
  148. struct spi_transfer *xfer = &ks->spi_xfer1;
  149. struct spi_message *msg = &ks->spi_msg1;
  150. __le16 txb[2];
  151. int ret;
  152. txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
  153. txb[1] = cpu_to_le16(val);
  154. xfer->tx_buf = txb;
  155. xfer->rx_buf = NULL;
  156. xfer->len = 4;
  157. ret = spi_sync(ks->spidev, msg);
  158. if (ret < 0)
  159. netdev_err(ks->netdev, "spi_sync() failed\n");
  160. }
  161. /**
  162. * ks8851_wrreg8 - write 8bit register value to chip
  163. * @ks: The chip state
  164. * @reg: The register address
  165. * @val: The value to write
  166. *
  167. * Issue a write to put the value @val into the register specified in @reg.
  168. */
  169. static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
  170. {
  171. struct spi_transfer *xfer = &ks->spi_xfer1;
  172. struct spi_message *msg = &ks->spi_msg1;
  173. __le16 txb[2];
  174. int ret;
  175. int bit;
  176. bit = 1 << (reg & 3);
  177. txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
  178. txb[1] = val;
  179. xfer->tx_buf = txb;
  180. xfer->rx_buf = NULL;
  181. xfer->len = 3;
  182. ret = spi_sync(ks->spidev, msg);
  183. if (ret < 0)
  184. netdev_err(ks->netdev, "spi_sync() failed\n");
  185. }
  186. /**
  187. * ks8851_rx_1msg - select whether to use one or two messages for spi read
  188. * @ks: The device structure
  189. *
  190. * Return whether to generate a single message with a tx and rx buffer
  191. * supplied to spi_sync(), or alternatively send the tx and rx buffers
  192. * as separate messages.
  193. *
  194. * Depending on the hardware in use, a single message may be more efficient
  195. * on interrupts or work done by the driver.
  196. *
  197. * This currently always returns true until we add some per-device data passed
  198. * from the platform code to specify which mode is better.
  199. */
  200. static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
  201. {
  202. return true;
  203. }
  204. /**
  205. * ks8851_rdreg - issue read register command and return the data
  206. * @ks: The device state
  207. * @op: The register address and byte enables in message format.
  208. * @rxb: The RX buffer to return the result into
  209. * @rxl: The length of data expected.
  210. *
  211. * This is the low level read call that issues the necessary spi message(s)
  212. * to read data from the register specified in @op.
  213. */
  214. static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
  215. u8 *rxb, unsigned rxl)
  216. {
  217. struct spi_transfer *xfer;
  218. struct spi_message *msg;
  219. __le16 *txb = (__le16 *)ks->txd;
  220. u8 *trx = ks->rxd;
  221. int ret;
  222. txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
  223. if (ks8851_rx_1msg(ks)) {
  224. msg = &ks->spi_msg1;
  225. xfer = &ks->spi_xfer1;
  226. xfer->tx_buf = txb;
  227. xfer->rx_buf = trx;
  228. xfer->len = rxl + 2;
  229. } else {
  230. msg = &ks->spi_msg2;
  231. xfer = ks->spi_xfer2;
  232. xfer->tx_buf = txb;
  233. xfer->rx_buf = NULL;
  234. xfer->len = 2;
  235. xfer++;
  236. xfer->tx_buf = NULL;
  237. xfer->rx_buf = trx;
  238. xfer->len = rxl;
  239. }
  240. ret = spi_sync(ks->spidev, msg);
  241. if (ret < 0)
  242. netdev_err(ks->netdev, "read: spi_sync() failed\n");
  243. else if (ks8851_rx_1msg(ks))
  244. memcpy(rxb, trx + 2, rxl);
  245. else
  246. memcpy(rxb, trx, rxl);
  247. }
  248. /**
  249. * ks8851_rdreg8 - read 8 bit register from device
  250. * @ks: The chip information
  251. * @reg: The register address
  252. *
  253. * Read a 8bit register from the chip, returning the result
  254. */
  255. static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
  256. {
  257. u8 rxb[1];
  258. ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
  259. return rxb[0];
  260. }
  261. /**
  262. * ks8851_rdreg16 - read 16 bit register from device
  263. * @ks: The chip information
  264. * @reg: The register address
  265. *
  266. * Read a 16bit register from the chip, returning the result
  267. */
  268. static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
  269. {
  270. __le16 rx = 0;
  271. ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
  272. return le16_to_cpu(rx);
  273. }
  274. /**
  275. * ks8851_rdreg32 - read 32 bit register from device
  276. * @ks: The chip information
  277. * @reg: The register address
  278. *
  279. * Read a 32bit register from the chip.
  280. *
  281. * Note, this read requires the address be aligned to 4 bytes.
  282. */
  283. static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
  284. {
  285. __le32 rx = 0;
  286. WARN_ON(reg & 3);
  287. ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
  288. return le32_to_cpu(rx);
  289. }
  290. /**
  291. * ks8851_soft_reset - issue one of the soft reset to the device
  292. * @ks: The device state.
  293. * @op: The bit(s) to set in the GRR
  294. *
  295. * Issue the relevant soft-reset command to the device's GRR register
  296. * specified by @op.
  297. *
  298. * Note, the delays are in there as a caution to ensure that the reset
  299. * has time to take effect and then complete. Since the datasheet does
  300. * not currently specify the exact sequence, we have chosen something
  301. * that seems to work with our device.
  302. */
  303. static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
  304. {
  305. ks8851_wrreg16(ks, KS_GRR, op);
  306. mdelay(1); /* wait a short time to effect reset */
  307. ks8851_wrreg16(ks, KS_GRR, 0);
  308. mdelay(1); /* wait for condition to clear */
  309. }
  310. /**
  311. * ks8851_set_powermode - set power mode of the device
  312. * @ks: The device state
  313. * @pwrmode: The power mode value to write to KS_PMECR.
  314. *
  315. * Change the power mode of the chip.
  316. */
  317. static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
  318. {
  319. unsigned pmecr;
  320. netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
  321. pmecr = ks8851_rdreg16(ks, KS_PMECR);
  322. pmecr &= ~PMECR_PM_MASK;
  323. pmecr |= pwrmode;
  324. ks8851_wrreg16(ks, KS_PMECR, pmecr);
  325. }
  326. /**
  327. * ks8851_write_mac_addr - write mac address to device registers
  328. * @dev: The network device
  329. *
  330. * Update the KS8851 MAC address registers from the address in @dev.
  331. *
  332. * This call assumes that the chip is not running, so there is no need to
  333. * shutdown the RXQ process whilst setting this.
  334. */
  335. static int ks8851_write_mac_addr(struct net_device *dev)
  336. {
  337. struct ks8851_net *ks = netdev_priv(dev);
  338. int i;
  339. mutex_lock(&ks->lock);
  340. /*
  341. * Wake up chip in case it was powered off when stopped; otherwise,
  342. * the first write to the MAC address does not take effect.
  343. */
  344. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  345. for (i = 0; i < ETH_ALEN; i++)
  346. ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
  347. if (!netif_running(dev))
  348. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  349. mutex_unlock(&ks->lock);
  350. return 0;
  351. }
  352. /**
  353. * ks8851_read_mac_addr - read mac address from device registers
  354. * @dev: The network device
  355. *
  356. * Update our copy of the KS8851 MAC address from the registers of @dev.
  357. */
  358. static void ks8851_read_mac_addr(struct net_device *dev)
  359. {
  360. struct ks8851_net *ks = netdev_priv(dev);
  361. int i;
  362. mutex_lock(&ks->lock);
  363. for (i = 0; i < ETH_ALEN; i++)
  364. dev->dev_addr[i] = ks8851_rdreg8(ks, KS_MAR(i));
  365. mutex_unlock(&ks->lock);
  366. }
  367. /**
  368. * ks8851_init_mac - initialise the mac address
  369. * @ks: The device structure
  370. *
  371. * Get or create the initial mac address for the device and then set that
  372. * into the station address register. If there is an EEPROM present, then
  373. * we try that. If no valid mac address is found we use eth_random_addr()
  374. * to create a new one.
  375. */
  376. static void ks8851_init_mac(struct ks8851_net *ks)
  377. {
  378. struct net_device *dev = ks->netdev;
  379. /* first, try reading what we've got already */
  380. if (ks->rc_ccr & CCR_EEPROM) {
  381. ks8851_read_mac_addr(dev);
  382. if (is_valid_ether_addr(dev->dev_addr))
  383. return;
  384. netdev_err(ks->netdev, "invalid mac address read %pM\n",
  385. dev->dev_addr);
  386. }
  387. eth_hw_addr_random(dev);
  388. ks8851_write_mac_addr(dev);
  389. }
  390. /**
  391. * ks8851_rdfifo - read data from the receive fifo
  392. * @ks: The device state.
  393. * @buff: The buffer address
  394. * @len: The length of the data to read
  395. *
  396. * Issue an RXQ FIFO read command and read the @len amount of data from
  397. * the FIFO into the buffer specified by @buff.
  398. */
  399. static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
  400. {
  401. struct spi_transfer *xfer = ks->spi_xfer2;
  402. struct spi_message *msg = &ks->spi_msg2;
  403. u8 txb[1];
  404. int ret;
  405. netif_dbg(ks, rx_status, ks->netdev,
  406. "%s: %d@%p\n", __func__, len, buff);
  407. /* set the operation we're issuing */
  408. txb[0] = KS_SPIOP_RXFIFO;
  409. xfer->tx_buf = txb;
  410. xfer->rx_buf = NULL;
  411. xfer->len = 1;
  412. xfer++;
  413. xfer->rx_buf = buff;
  414. xfer->tx_buf = NULL;
  415. xfer->len = len;
  416. ret = spi_sync(ks->spidev, msg);
  417. if (ret < 0)
  418. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  419. }
  420. /**
  421. * ks8851_dbg_dumpkkt - dump initial packet contents to debug
  422. * @ks: The device state
  423. * @rxpkt: The data for the received packet
  424. *
  425. * Dump the initial data from the packet to dev_dbg().
  426. */
  427. static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
  428. {
  429. netdev_dbg(ks->netdev,
  430. "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
  431. rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
  432. rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
  433. rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
  434. }
  435. /**
  436. * ks8851_rx_pkts - receive packets from the host
  437. * @ks: The device information.
  438. *
  439. * This is called from the IRQ work queue when the system detects that there
  440. * are packets in the receive queue. Find out how many packets there are and
  441. * read them from the FIFO.
  442. */
  443. static void ks8851_rx_pkts(struct ks8851_net *ks)
  444. {
  445. struct sk_buff *skb;
  446. unsigned rxfc;
  447. unsigned rxlen;
  448. unsigned rxstat;
  449. u32 rxh;
  450. u8 *rxpkt;
  451. rxfc = ks8851_rdreg8(ks, KS_RXFC);
  452. netif_dbg(ks, rx_status, ks->netdev,
  453. "%s: %d packets\n", __func__, rxfc);
  454. /* Currently we're issuing a read per packet, but we could possibly
  455. * improve the code by issuing a single read, getting the receive
  456. * header, allocating the packet and then reading the packet data
  457. * out in one go.
  458. *
  459. * This form of operation would require us to hold the SPI bus'
  460. * chipselect low during the entie transaction to avoid any
  461. * reset to the data stream coming from the chip.
  462. */
  463. for (; rxfc != 0; rxfc--) {
  464. rxh = ks8851_rdreg32(ks, KS_RXFHSR);
  465. rxstat = rxh & 0xffff;
  466. rxlen = (rxh >> 16) & 0xfff;
  467. netif_dbg(ks, rx_status, ks->netdev,
  468. "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
  469. /* the length of the packet includes the 32bit CRC */
  470. /* set dma read address */
  471. ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
  472. /* start the packet dma process, and set auto-dequeue rx */
  473. ks8851_wrreg16(ks, KS_RXQCR,
  474. ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
  475. if (rxlen > 4) {
  476. unsigned int rxalign;
  477. rxlen -= 4;
  478. rxalign = ALIGN(rxlen, 4);
  479. skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign);
  480. if (skb) {
  481. /* 4 bytes of status header + 4 bytes of
  482. * garbage: we put them before ethernet
  483. * header, so that they are copied,
  484. * but ignored.
  485. */
  486. rxpkt = skb_put(skb, rxlen) - 8;
  487. ks8851_rdfifo(ks, rxpkt, rxalign + 8);
  488. if (netif_msg_pktdata(ks))
  489. ks8851_dbg_dumpkkt(ks, rxpkt);
  490. skb->protocol = eth_type_trans(skb, ks->netdev);
  491. netif_rx_ni(skb);
  492. ks->netdev->stats.rx_packets++;
  493. ks->netdev->stats.rx_bytes += rxlen;
  494. }
  495. }
  496. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  497. }
  498. }
  499. /**
  500. * ks8851_irq - IRQ handler for dealing with interrupt requests
  501. * @irq: IRQ number
  502. * @_ks: cookie
  503. *
  504. * This handler is invoked when the IRQ line asserts to find out what happened.
  505. * As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs
  506. * in thread context.
  507. *
  508. * Read the interrupt status, work out what needs to be done and then clear
  509. * any of the interrupts that are not needed.
  510. */
  511. static irqreturn_t ks8851_irq(int irq, void *_ks)
  512. {
  513. struct ks8851_net *ks = _ks;
  514. unsigned status;
  515. unsigned handled = 0;
  516. mutex_lock(&ks->lock);
  517. status = ks8851_rdreg16(ks, KS_ISR);
  518. netif_dbg(ks, intr, ks->netdev,
  519. "%s: status 0x%04x\n", __func__, status);
  520. if (status & IRQ_LCI)
  521. handled |= IRQ_LCI;
  522. if (status & IRQ_LDI) {
  523. u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
  524. pmecr &= ~PMECR_WKEVT_MASK;
  525. ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
  526. handled |= IRQ_LDI;
  527. }
  528. if (status & IRQ_RXPSI)
  529. handled |= IRQ_RXPSI;
  530. if (status & IRQ_TXI) {
  531. handled |= IRQ_TXI;
  532. /* no lock here, tx queue should have been stopped */
  533. /* update our idea of how much tx space is available to the
  534. * system */
  535. ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
  536. netif_dbg(ks, intr, ks->netdev,
  537. "%s: txspace %d\n", __func__, ks->tx_space);
  538. }
  539. if (status & IRQ_RXI)
  540. handled |= IRQ_RXI;
  541. if (status & IRQ_SPIBEI) {
  542. dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
  543. handled |= IRQ_SPIBEI;
  544. }
  545. ks8851_wrreg16(ks, KS_ISR, handled);
  546. if (status & IRQ_RXI) {
  547. /* the datasheet says to disable the rx interrupt during
  548. * packet read-out, however we're masking the interrupt
  549. * from the device so do not bother masking just the RX
  550. * from the device. */
  551. ks8851_rx_pkts(ks);
  552. }
  553. /* if something stopped the rx process, probably due to wanting
  554. * to change the rx settings, then do something about restarting
  555. * it. */
  556. if (status & IRQ_RXPSI) {
  557. struct ks8851_rxctrl *rxc = &ks->rxctrl;
  558. /* update the multicast hash table */
  559. ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
  560. ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
  561. ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
  562. ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
  563. ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
  564. ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
  565. }
  566. mutex_unlock(&ks->lock);
  567. if (status & IRQ_LCI)
  568. mii_check_link(&ks->mii);
  569. if (status & IRQ_TXI)
  570. netif_wake_queue(ks->netdev);
  571. return IRQ_HANDLED;
  572. }
  573. /**
  574. * calc_txlen - calculate size of message to send packet
  575. * @len: Length of data
  576. *
  577. * Returns the size of the TXFIFO message needed to send
  578. * this packet.
  579. */
  580. static inline unsigned calc_txlen(unsigned len)
  581. {
  582. return ALIGN(len + 4, 4);
  583. }
  584. /**
  585. * ks8851_wrpkt - write packet to TX FIFO
  586. * @ks: The device state.
  587. * @txp: The sk_buff to transmit.
  588. * @irq: IRQ on completion of the packet.
  589. *
  590. * Send the @txp to the chip. This means creating the relevant packet header
  591. * specifying the length of the packet and the other information the chip
  592. * needs, such as IRQ on completion. Send the header and the packet data to
  593. * the device.
  594. */
  595. static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
  596. {
  597. struct spi_transfer *xfer = ks->spi_xfer2;
  598. struct spi_message *msg = &ks->spi_msg2;
  599. unsigned fid = 0;
  600. int ret;
  601. netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n",
  602. __func__, txp, txp->len, txp->data, irq);
  603. fid = ks->fid++;
  604. fid &= TXFR_TXFID_MASK;
  605. if (irq)
  606. fid |= TXFR_TXIC; /* irq on completion */
  607. /* start header at txb[1] to align txw entries */
  608. ks->txh.txb[1] = KS_SPIOP_TXFIFO;
  609. ks->txh.txw[1] = cpu_to_le16(fid);
  610. ks->txh.txw[2] = cpu_to_le16(txp->len);
  611. xfer->tx_buf = &ks->txh.txb[1];
  612. xfer->rx_buf = NULL;
  613. xfer->len = 5;
  614. xfer++;
  615. xfer->tx_buf = txp->data;
  616. xfer->rx_buf = NULL;
  617. xfer->len = ALIGN(txp->len, 4);
  618. ret = spi_sync(ks->spidev, msg);
  619. if (ret < 0)
  620. netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__);
  621. }
  622. /**
  623. * ks8851_done_tx - update and then free skbuff after transmitting
  624. * @ks: The device state
  625. * @txb: The buffer transmitted
  626. */
  627. static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
  628. {
  629. struct net_device *dev = ks->netdev;
  630. dev->stats.tx_bytes += txb->len;
  631. dev->stats.tx_packets++;
  632. dev_kfree_skb(txb);
  633. }
  634. /**
  635. * ks8851_tx_work - process tx packet(s)
  636. * @work: The work strucutre what was scheduled.
  637. *
  638. * This is called when a number of packets have been scheduled for
  639. * transmission and need to be sent to the device.
  640. */
  641. static void ks8851_tx_work(struct work_struct *work)
  642. {
  643. struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
  644. struct sk_buff *txb;
  645. bool last = skb_queue_empty(&ks->txq);
  646. mutex_lock(&ks->lock);
  647. while (!last) {
  648. txb = skb_dequeue(&ks->txq);
  649. last = skb_queue_empty(&ks->txq);
  650. if (txb != NULL) {
  651. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  652. ks8851_wrpkt(ks, txb, last);
  653. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  654. ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
  655. ks8851_done_tx(ks, txb);
  656. }
  657. }
  658. mutex_unlock(&ks->lock);
  659. }
  660. /**
  661. * ks8851_net_open - open network device
  662. * @dev: The network device being opened.
  663. *
  664. * Called when the network device is marked active, such as a user executing
  665. * 'ifconfig up' on the device.
  666. */
  667. static int ks8851_net_open(struct net_device *dev)
  668. {
  669. struct ks8851_net *ks = netdev_priv(dev);
  670. /* lock the card, even if we may not actually be doing anything
  671. * else at the moment */
  672. mutex_lock(&ks->lock);
  673. netif_dbg(ks, ifup, ks->netdev, "opening\n");
  674. /* bring chip out of any power saving mode it was in */
  675. ks8851_set_powermode(ks, PMECR_PM_NORMAL);
  676. /* issue a soft reset to the RX/TX QMU to put it into a known
  677. * state. */
  678. ks8851_soft_reset(ks, GRR_QMU);
  679. /* setup transmission parameters */
  680. ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */
  681. TXCR_TXPE | /* pad to min length */
  682. TXCR_TXCRC | /* add CRC */
  683. TXCR_TXFCE)); /* enable flow control */
  684. /* auto-increment tx data, reset tx pointer */
  685. ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
  686. /* setup receiver control */
  687. ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */
  688. RXCR1_RXFCE | /* enable flow control */
  689. RXCR1_RXBE | /* broadcast enable */
  690. RXCR1_RXUE | /* unicast enable */
  691. RXCR1_RXE)); /* enable rx block */
  692. /* transfer entire frames out in one go */
  693. ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
  694. /* set receive counter timeouts */
  695. ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */
  696. ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */
  697. ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */
  698. ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */
  699. RXQCR_RXDBCTE | /* IRQ on byte count exceeded */
  700. RXQCR_RXDTTE); /* IRQ on time exceeded */
  701. ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  702. /* clear then enable interrupts */
  703. #define STD_IRQ (IRQ_LCI | /* Link Change */ \
  704. IRQ_TXI | /* TX done */ \
  705. IRQ_RXI | /* RX done */ \
  706. IRQ_SPIBEI | /* SPI bus error */ \
  707. IRQ_TXPSI | /* TX process stop */ \
  708. IRQ_RXPSI) /* RX process stop */
  709. ks->rc_ier = STD_IRQ;
  710. ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
  711. ks8851_wrreg16(ks, KS_IER, STD_IRQ);
  712. netif_start_queue(ks->netdev);
  713. netif_dbg(ks, ifup, ks->netdev, "network device up\n");
  714. mutex_unlock(&ks->lock);
  715. return 0;
  716. }
  717. /**
  718. * ks8851_net_stop - close network device
  719. * @dev: The device being closed.
  720. *
  721. * Called to close down a network device which has been active. Cancell any
  722. * work, shutdown the RX and TX process and then place the chip into a low
  723. * power state whilst it is not being used.
  724. */
  725. static int ks8851_net_stop(struct net_device *dev)
  726. {
  727. struct ks8851_net *ks = netdev_priv(dev);
  728. netif_info(ks, ifdown, dev, "shutting down\n");
  729. netif_stop_queue(dev);
  730. mutex_lock(&ks->lock);
  731. /* turn off the IRQs and ack any outstanding */
  732. ks8851_wrreg16(ks, KS_IER, 0x0000);
  733. ks8851_wrreg16(ks, KS_ISR, 0xffff);
  734. mutex_unlock(&ks->lock);
  735. /* stop any outstanding work */
  736. flush_work(&ks->tx_work);
  737. flush_work(&ks->rxctrl_work);
  738. mutex_lock(&ks->lock);
  739. /* shutdown RX process */
  740. ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
  741. /* shutdown TX process */
  742. ks8851_wrreg16(ks, KS_TXCR, 0x0000);
  743. /* set powermode to soft power down to save power */
  744. ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
  745. mutex_unlock(&ks->lock);
  746. /* ensure any queued tx buffers are dumped */
  747. while (!skb_queue_empty(&ks->txq)) {
  748. struct sk_buff *txb = skb_dequeue(&ks->txq);
  749. netif_dbg(ks, ifdown, ks->netdev,
  750. "%s: freeing txb %p\n", __func__, txb);
  751. dev_kfree_skb(txb);
  752. }
  753. return 0;
  754. }
  755. /**
  756. * ks8851_start_xmit - transmit packet
  757. * @skb: The buffer to transmit
  758. * @dev: The device used to transmit the packet.
  759. *
  760. * Called by the network layer to transmit the @skb. Queue the packet for
  761. * the device and schedule the necessary work to transmit the packet when
  762. * it is free.
  763. *
  764. * We do this to firstly avoid sleeping with the network device locked,
  765. * and secondly so we can round up more than one packet to transmit which
  766. * means we can try and avoid generating too many transmit done interrupts.
  767. */
  768. static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
  769. struct net_device *dev)
  770. {
  771. struct ks8851_net *ks = netdev_priv(dev);
  772. unsigned needed = calc_txlen(skb->len);
  773. netdev_tx_t ret = NETDEV_TX_OK;
  774. netif_dbg(ks, tx_queued, ks->netdev,
  775. "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
  776. spin_lock(&ks->statelock);
  777. if (needed > ks->tx_space) {
  778. netif_stop_queue(dev);
  779. ret = NETDEV_TX_BUSY;
  780. } else {
  781. ks->tx_space -= needed;
  782. skb_queue_tail(&ks->txq, skb);
  783. }
  784. spin_unlock(&ks->statelock);
  785. schedule_work(&ks->tx_work);
  786. return ret;
  787. }
  788. /**
  789. * ks8851_rxctrl_work - work handler to change rx mode
  790. * @work: The work structure this belongs to.
  791. *
  792. * Lock the device and issue the necessary changes to the receive mode from
  793. * the network device layer. This is done so that we can do this without
  794. * having to sleep whilst holding the network device lock.
  795. *
  796. * Since the recommendation from Micrel is that the RXQ is shutdown whilst the
  797. * receive parameters are programmed, we issue a write to disable the RXQ and
  798. * then wait for the interrupt handler to be triggered once the RXQ shutdown is
  799. * complete. The interrupt handler then writes the new values into the chip.
  800. */
  801. static void ks8851_rxctrl_work(struct work_struct *work)
  802. {
  803. struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
  804. mutex_lock(&ks->lock);
  805. /* need to shutdown RXQ before modifying filter parameters */
  806. ks8851_wrreg16(ks, KS_RXCR1, 0x00);
  807. mutex_unlock(&ks->lock);
  808. }
  809. static void ks8851_set_rx_mode(struct net_device *dev)
  810. {
  811. struct ks8851_net *ks = netdev_priv(dev);
  812. struct ks8851_rxctrl rxctrl;
  813. memset(&rxctrl, 0, sizeof(rxctrl));
  814. if (dev->flags & IFF_PROMISC) {
  815. /* interface to receive everything */
  816. rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
  817. } else if (dev->flags & IFF_ALLMULTI) {
  818. /* accept all multicast packets */
  819. rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
  820. RXCR1_RXPAFMA | RXCR1_RXMAFMA);
  821. } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) {
  822. struct netdev_hw_addr *ha;
  823. u32 crc;
  824. /* accept some multicast */
  825. netdev_for_each_mc_addr(ha, dev) {
  826. crc = ether_crc(ETH_ALEN, ha->addr);
  827. crc >>= (32 - 6); /* get top six bits */
  828. rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
  829. }
  830. rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
  831. } else {
  832. /* just accept broadcast / unicast */
  833. rxctrl.rxcr1 = RXCR1_RXPAFMA;
  834. }
  835. rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */
  836. RXCR1_RXBE | /* broadcast enable */
  837. RXCR1_RXE | /* RX process enable */
  838. RXCR1_RXFCE); /* enable flow control */
  839. rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
  840. /* schedule work to do the actual set of the data if needed */
  841. spin_lock(&ks->statelock);
  842. if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
  843. memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
  844. schedule_work(&ks->rxctrl_work);
  845. }
  846. spin_unlock(&ks->statelock);
  847. }
  848. static int ks8851_set_mac_address(struct net_device *dev, void *addr)
  849. {
  850. struct sockaddr *sa = addr;
  851. if (netif_running(dev))
  852. return -EBUSY;
  853. if (!is_valid_ether_addr(sa->sa_data))
  854. return -EADDRNOTAVAIL;
  855. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  856. return ks8851_write_mac_addr(dev);
  857. }
  858. static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  859. {
  860. struct ks8851_net *ks = netdev_priv(dev);
  861. if (!netif_running(dev))
  862. return -EINVAL;
  863. return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
  864. }
  865. static const struct net_device_ops ks8851_netdev_ops = {
  866. .ndo_open = ks8851_net_open,
  867. .ndo_stop = ks8851_net_stop,
  868. .ndo_do_ioctl = ks8851_net_ioctl,
  869. .ndo_start_xmit = ks8851_start_xmit,
  870. .ndo_set_mac_address = ks8851_set_mac_address,
  871. .ndo_set_rx_mode = ks8851_set_rx_mode,
  872. .ndo_change_mtu = eth_change_mtu,
  873. .ndo_validate_addr = eth_validate_addr,
  874. };
  875. /* ethtool support */
  876. static void ks8851_get_drvinfo(struct net_device *dev,
  877. struct ethtool_drvinfo *di)
  878. {
  879. strlcpy(di->driver, "KS8851", sizeof(di->driver));
  880. strlcpy(di->version, "1.00", sizeof(di->version));
  881. strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
  882. }
  883. static u32 ks8851_get_msglevel(struct net_device *dev)
  884. {
  885. struct ks8851_net *ks = netdev_priv(dev);
  886. return ks->msg_enable;
  887. }
  888. static void ks8851_set_msglevel(struct net_device *dev, u32 to)
  889. {
  890. struct ks8851_net *ks = netdev_priv(dev);
  891. ks->msg_enable = to;
  892. }
  893. static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  894. {
  895. struct ks8851_net *ks = netdev_priv(dev);
  896. return mii_ethtool_gset(&ks->mii, cmd);
  897. }
  898. static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  899. {
  900. struct ks8851_net *ks = netdev_priv(dev);
  901. return mii_ethtool_sset(&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_settings = ks8851_get_settings,
  1023. .set_settings = ks8851_set_settings,
  1024. .get_link = ks8851_get_link,
  1025. .nway_reset = ks8851_nway_reset,
  1026. .get_eeprom_len = ks8851_get_eeprom_len,
  1027. .get_eeprom = ks8851_get_eeprom,
  1028. .set_eeprom = ks8851_set_eeprom,
  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. ndev->if_port = IF_PORT_100BASET;
  1235. ndev->netdev_ops = &ks8851_netdev_ops;
  1236. ndev->irq = spi->irq;
  1237. /* issue a global soft reset to reset the device. */
  1238. ks8851_soft_reset(ks, GRR_GSR);
  1239. /* simple check for a valid chip being connected to the bus */
  1240. cider = ks8851_rdreg16(ks, KS_CIDER);
  1241. if ((cider & ~CIDER_REV_MASK) != CIDER_ID) {
  1242. dev_err(&spi->dev, "failed to read device ID\n");
  1243. ret = -ENODEV;
  1244. goto err_id;
  1245. }
  1246. /* cache the contents of the CCR register for EEPROM, etc. */
  1247. ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR);
  1248. if (ks->rc_ccr & CCR_EEPROM)
  1249. ks->eeprom_size = 128;
  1250. else
  1251. ks->eeprom_size = 0;
  1252. ks8851_read_selftest(ks);
  1253. ks8851_init_mac(ks);
  1254. ret = request_threaded_irq(spi->irq, NULL, ks8851_irq,
  1255. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  1256. ndev->name, ks);
  1257. if (ret < 0) {
  1258. dev_err(&spi->dev, "failed to get irq\n");
  1259. goto err_irq;
  1260. }
  1261. ret = register_netdev(ndev);
  1262. if (ret) {
  1263. dev_err(&spi->dev, "failed to register network device\n");
  1264. goto err_netdev;
  1265. }
  1266. netdev_info(ndev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
  1267. CIDER_REV_GET(cider), ndev->dev_addr, ndev->irq,
  1268. ks->rc_ccr & CCR_EEPROM ? "has" : "no");
  1269. return 0;
  1270. err_netdev:
  1271. free_irq(ndev->irq, ks);
  1272. err_irq:
  1273. if (gpio_is_valid(gpio))
  1274. gpio_set_value(gpio, 0);
  1275. err_id:
  1276. regulator_disable(ks->vdd_reg);
  1277. err_reg:
  1278. regulator_disable(ks->vdd_io);
  1279. err_reg_io:
  1280. err_gpio:
  1281. free_netdev(ndev);
  1282. return ret;
  1283. }
  1284. static int ks8851_remove(struct spi_device *spi)
  1285. {
  1286. struct ks8851_net *priv = spi_get_drvdata(spi);
  1287. if (netif_msg_drv(priv))
  1288. dev_info(&spi->dev, "remove\n");
  1289. unregister_netdev(priv->netdev);
  1290. free_irq(spi->irq, priv);
  1291. if (gpio_is_valid(priv->gpio))
  1292. gpio_set_value(priv->gpio, 0);
  1293. regulator_disable(priv->vdd_reg);
  1294. regulator_disable(priv->vdd_io);
  1295. free_netdev(priv->netdev);
  1296. return 0;
  1297. }
  1298. static const struct of_device_id ks8851_match_table[] = {
  1299. { .compatible = "micrel,ks8851" },
  1300. { }
  1301. };
  1302. MODULE_DEVICE_TABLE(of, ks8851_match_table);
  1303. static struct spi_driver ks8851_driver = {
  1304. .driver = {
  1305. .name = "ks8851",
  1306. .of_match_table = ks8851_match_table,
  1307. .pm = &ks8851_pm_ops,
  1308. },
  1309. .probe = ks8851_probe,
  1310. .remove = ks8851_remove,
  1311. };
  1312. module_spi_driver(ks8851_driver);
  1313. MODULE_DESCRIPTION("KS8851 Network driver");
  1314. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  1315. MODULE_LICENSE("GPL");
  1316. module_param_named(message, msg_enable, int, 0);
  1317. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
  1318. MODULE_ALIAS("spi:ks8851");