korina.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Driver for the IDT RC32434 (Korina) on-chip ethernet controller.
  3. *
  4. * Copyright 2004 IDT Inc. (rischelp@idt.com)
  5. * Copyright 2006 Felix Fietkau <nbd@openwrt.org>
  6. * Copyright 2008 Florian Fainelli <florian@openwrt.org>
  7. * Copyright 2017 Roman Yeryomin <roman@advem.lv>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  17. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  20. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. * Writing to a DMA status register:
  30. *
  31. * When writing to the status register, you should mask the bit you have
  32. * been testing the status register with. Both Tx and Rx DMA registers
  33. * should stick to this procedure.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/kernel.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/sched.h>
  39. #include <linux/ctype.h>
  40. #include <linux/types.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/ioport.h>
  43. #include <linux/in.h>
  44. #include <linux/slab.h>
  45. #include <linux/string.h>
  46. #include <linux/delay.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/etherdevice.h>
  49. #include <linux/skbuff.h>
  50. #include <linux/errno.h>
  51. #include <linux/platform_device.h>
  52. #include <linux/mii.h>
  53. #include <linux/ethtool.h>
  54. #include <linux/crc32.h>
  55. #include <asm/bootinfo.h>
  56. #include <asm/bitops.h>
  57. #include <asm/pgtable.h>
  58. #include <asm/io.h>
  59. #include <asm/dma.h>
  60. #include <asm/mach-rc32434/rb.h>
  61. #include <asm/mach-rc32434/rc32434.h>
  62. #include <asm/mach-rc32434/eth.h>
  63. #include <asm/mach-rc32434/dma_v.h>
  64. #define DRV_NAME "korina"
  65. #define DRV_VERSION "0.20"
  66. #define DRV_RELDATE "15Sep2017"
  67. #define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \
  68. ((dev)->dev_addr[1]))
  69. #define STATION_ADDRESS_LOW(dev) (((dev)->dev_addr[2] << 24) | \
  70. ((dev)->dev_addr[3] << 16) | \
  71. ((dev)->dev_addr[4] << 8) | \
  72. ((dev)->dev_addr[5]))
  73. #define MII_CLOCK 1250000 /* no more than 2.5MHz */
  74. /* the following must be powers of two */
  75. #define KORINA_NUM_RDS 64 /* number of receive descriptors */
  76. #define KORINA_NUM_TDS 64 /* number of transmit descriptors */
  77. /* KORINA_RBSIZE is the hardware's default maximum receive
  78. * frame size in bytes. Having this hardcoded means that there
  79. * is no support for MTU sizes greater than 1500. */
  80. #define KORINA_RBSIZE 1536 /* size of one resource buffer = Ether MTU */
  81. #define KORINA_RDS_MASK (KORINA_NUM_RDS - 1)
  82. #define KORINA_TDS_MASK (KORINA_NUM_TDS - 1)
  83. #define RD_RING_SIZE (KORINA_NUM_RDS * sizeof(struct dma_desc))
  84. #define TD_RING_SIZE (KORINA_NUM_TDS * sizeof(struct dma_desc))
  85. #define TX_TIMEOUT (6000 * HZ / 1000)
  86. enum chain_status {
  87. desc_filled,
  88. desc_empty
  89. };
  90. #define IS_DMA_FINISHED(X) (((X) & (DMA_DESC_FINI)) != 0)
  91. #define IS_DMA_DONE(X) (((X) & (DMA_DESC_DONE)) != 0)
  92. #define RCVPKT_LENGTH(X) (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT)
  93. /* Information that need to be kept for each board. */
  94. struct korina_private {
  95. struct eth_regs *eth_regs;
  96. struct dma_reg *rx_dma_regs;
  97. struct dma_reg *tx_dma_regs;
  98. struct dma_desc *td_ring; /* transmit descriptor ring */
  99. struct dma_desc *rd_ring; /* receive descriptor ring */
  100. struct sk_buff *tx_skb[KORINA_NUM_TDS];
  101. struct sk_buff *rx_skb[KORINA_NUM_RDS];
  102. int rx_next_done;
  103. int rx_chain_head;
  104. int rx_chain_tail;
  105. enum chain_status rx_chain_status;
  106. int tx_next_done;
  107. int tx_chain_head;
  108. int tx_chain_tail;
  109. enum chain_status tx_chain_status;
  110. int tx_count;
  111. int tx_full;
  112. int rx_irq;
  113. int tx_irq;
  114. spinlock_t lock; /* NIC xmit lock */
  115. int dma_halt_cnt;
  116. int dma_run_cnt;
  117. struct napi_struct napi;
  118. struct timer_list media_check_timer;
  119. struct mii_if_info mii_if;
  120. struct work_struct restart_task;
  121. struct net_device *dev;
  122. int phy_addr;
  123. };
  124. extern unsigned int idt_cpu_freq;
  125. static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr)
  126. {
  127. writel(0, &ch->dmandptr);
  128. writel(dma_addr, &ch->dmadptr);
  129. }
  130. static inline void korina_abort_dma(struct net_device *dev,
  131. struct dma_reg *ch)
  132. {
  133. if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) {
  134. writel(0x10, &ch->dmac);
  135. while (!(readl(&ch->dmas) & DMA_STAT_HALT))
  136. netif_trans_update(dev);
  137. writel(0, &ch->dmas);
  138. }
  139. writel(0, &ch->dmadptr);
  140. writel(0, &ch->dmandptr);
  141. }
  142. static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr)
  143. {
  144. writel(dma_addr, &ch->dmandptr);
  145. }
  146. static void korina_abort_tx(struct net_device *dev)
  147. {
  148. struct korina_private *lp = netdev_priv(dev);
  149. korina_abort_dma(dev, lp->tx_dma_regs);
  150. }
  151. static void korina_abort_rx(struct net_device *dev)
  152. {
  153. struct korina_private *lp = netdev_priv(dev);
  154. korina_abort_dma(dev, lp->rx_dma_regs);
  155. }
  156. static void korina_start_rx(struct korina_private *lp,
  157. struct dma_desc *rd)
  158. {
  159. korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd));
  160. }
  161. static void korina_chain_rx(struct korina_private *lp,
  162. struct dma_desc *rd)
  163. {
  164. korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd));
  165. }
  166. /* transmit packet */
  167. static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
  168. {
  169. struct korina_private *lp = netdev_priv(dev);
  170. unsigned long flags;
  171. u32 length;
  172. u32 chain_prev, chain_next;
  173. struct dma_desc *td;
  174. spin_lock_irqsave(&lp->lock, flags);
  175. td = &lp->td_ring[lp->tx_chain_tail];
  176. /* stop queue when full, drop pkts if queue already full */
  177. if (lp->tx_count >= (KORINA_NUM_TDS - 2)) {
  178. lp->tx_full = 1;
  179. if (lp->tx_count == (KORINA_NUM_TDS - 2))
  180. netif_stop_queue(dev);
  181. else {
  182. dev->stats.tx_dropped++;
  183. dev_kfree_skb_any(skb);
  184. spin_unlock_irqrestore(&lp->lock, flags);
  185. return NETDEV_TX_BUSY;
  186. }
  187. }
  188. lp->tx_count++;
  189. lp->tx_skb[lp->tx_chain_tail] = skb;
  190. length = skb->len;
  191. dma_cache_wback((u32)skb->data, skb->len);
  192. /* Setup the transmit descriptor. */
  193. dma_cache_inv((u32) td, sizeof(*td));
  194. td->ca = CPHYSADDR(skb->data);
  195. chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK;
  196. chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK;
  197. if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
  198. if (lp->tx_chain_status == desc_empty) {
  199. /* Update tail */
  200. td->control = DMA_COUNT(length) |
  201. DMA_DESC_COF | DMA_DESC_IOF;
  202. /* Move tail */
  203. lp->tx_chain_tail = chain_next;
  204. /* Write to NDPTR */
  205. writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
  206. &lp->tx_dma_regs->dmandptr);
  207. /* Move head to tail */
  208. lp->tx_chain_head = lp->tx_chain_tail;
  209. } else {
  210. /* Update tail */
  211. td->control = DMA_COUNT(length) |
  212. DMA_DESC_COF | DMA_DESC_IOF;
  213. /* Link to prev */
  214. lp->td_ring[chain_prev].control &=
  215. ~DMA_DESC_COF;
  216. /* Link to prev */
  217. lp->td_ring[chain_prev].link = CPHYSADDR(td);
  218. /* Move tail */
  219. lp->tx_chain_tail = chain_next;
  220. /* Write to NDPTR */
  221. writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
  222. &(lp->tx_dma_regs->dmandptr));
  223. /* Move head to tail */
  224. lp->tx_chain_head = lp->tx_chain_tail;
  225. lp->tx_chain_status = desc_empty;
  226. }
  227. } else {
  228. if (lp->tx_chain_status == desc_empty) {
  229. /* Update tail */
  230. td->control = DMA_COUNT(length) |
  231. DMA_DESC_COF | DMA_DESC_IOF;
  232. /* Move tail */
  233. lp->tx_chain_tail = chain_next;
  234. lp->tx_chain_status = desc_filled;
  235. } else {
  236. /* Update tail */
  237. td->control = DMA_COUNT(length) |
  238. DMA_DESC_COF | DMA_DESC_IOF;
  239. lp->td_ring[chain_prev].control &=
  240. ~DMA_DESC_COF;
  241. lp->td_ring[chain_prev].link = CPHYSADDR(td);
  242. lp->tx_chain_tail = chain_next;
  243. }
  244. }
  245. dma_cache_wback((u32) td, sizeof(*td));
  246. netif_trans_update(dev);
  247. spin_unlock_irqrestore(&lp->lock, flags);
  248. return NETDEV_TX_OK;
  249. }
  250. static int mdio_read(struct net_device *dev, int mii_id, int reg)
  251. {
  252. struct korina_private *lp = netdev_priv(dev);
  253. int ret;
  254. mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
  255. writel(0, &lp->eth_regs->miimcfg);
  256. writel(0, &lp->eth_regs->miimcmd);
  257. writel(mii_id | reg, &lp->eth_regs->miimaddr);
  258. writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
  259. ret = (int)(readl(&lp->eth_regs->miimrdd));
  260. return ret;
  261. }
  262. static void mdio_write(struct net_device *dev, int mii_id, int reg, int val)
  263. {
  264. struct korina_private *lp = netdev_priv(dev);
  265. mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
  266. writel(0, &lp->eth_regs->miimcfg);
  267. writel(1, &lp->eth_regs->miimcmd);
  268. writel(mii_id | reg, &lp->eth_regs->miimaddr);
  269. writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
  270. writel(val, &lp->eth_regs->miimwtd);
  271. }
  272. /* Ethernet Rx DMA interrupt */
  273. static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
  274. {
  275. struct net_device *dev = dev_id;
  276. struct korina_private *lp = netdev_priv(dev);
  277. u32 dmas, dmasm;
  278. irqreturn_t retval;
  279. dmas = readl(&lp->rx_dma_regs->dmas);
  280. if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) {
  281. dmasm = readl(&lp->rx_dma_regs->dmasm);
  282. writel(dmasm | (DMA_STAT_DONE |
  283. DMA_STAT_HALT | DMA_STAT_ERR),
  284. &lp->rx_dma_regs->dmasm);
  285. napi_schedule(&lp->napi);
  286. if (dmas & DMA_STAT_ERR)
  287. printk(KERN_ERR "%s: DMA error\n", dev->name);
  288. retval = IRQ_HANDLED;
  289. } else
  290. retval = IRQ_NONE;
  291. return retval;
  292. }
  293. static int korina_rx(struct net_device *dev, int limit)
  294. {
  295. struct korina_private *lp = netdev_priv(dev);
  296. struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done];
  297. struct sk_buff *skb, *skb_new;
  298. u8 *pkt_buf;
  299. u32 devcs, pkt_len, dmas;
  300. int count;
  301. dma_cache_inv((u32)rd, sizeof(*rd));
  302. for (count = 0; count < limit; count++) {
  303. skb = lp->rx_skb[lp->rx_next_done];
  304. skb_new = NULL;
  305. devcs = rd->devcs;
  306. if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0)
  307. break;
  308. /* check that this is a whole packet
  309. * WARNING: DMA_FD bit incorrectly set
  310. * in Rc32434 (errata ref #077) */
  311. if (!(devcs & ETH_RX_LD))
  312. goto next;
  313. if (!(devcs & ETH_RX_ROK)) {
  314. /* Update statistics counters */
  315. dev->stats.rx_errors++;
  316. dev->stats.rx_dropped++;
  317. if (devcs & ETH_RX_CRC)
  318. dev->stats.rx_crc_errors++;
  319. if (devcs & ETH_RX_LE)
  320. dev->stats.rx_length_errors++;
  321. if (devcs & ETH_RX_OVR)
  322. dev->stats.rx_fifo_errors++;
  323. if (devcs & ETH_RX_CV)
  324. dev->stats.rx_frame_errors++;
  325. if (devcs & ETH_RX_CES)
  326. dev->stats.rx_frame_errors++;
  327. goto next;
  328. }
  329. pkt_len = RCVPKT_LENGTH(devcs);
  330. /* must be the (first and) last
  331. * descriptor then */
  332. pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data;
  333. /* invalidate the cache */
  334. dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
  335. /* Malloc up new buffer. */
  336. skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
  337. if (!skb_new)
  338. break;
  339. /* Do not count the CRC */
  340. skb_put(skb, pkt_len - 4);
  341. skb->protocol = eth_type_trans(skb, dev);
  342. /* Pass the packet to upper layers */
  343. napi_gro_receive(&lp->napi, skb);
  344. dev->stats.rx_packets++;
  345. dev->stats.rx_bytes += pkt_len;
  346. /* Update the mcast stats */
  347. if (devcs & ETH_RX_MP)
  348. dev->stats.multicast++;
  349. lp->rx_skb[lp->rx_next_done] = skb_new;
  350. next:
  351. rd->devcs = 0;
  352. /* Restore descriptor's curr_addr */
  353. if (skb_new)
  354. rd->ca = CPHYSADDR(skb_new->data);
  355. else
  356. rd->ca = CPHYSADDR(skb->data);
  357. rd->control = DMA_COUNT(KORINA_RBSIZE) |
  358. DMA_DESC_COD | DMA_DESC_IOD;
  359. lp->rd_ring[(lp->rx_next_done - 1) &
  360. KORINA_RDS_MASK].control &=
  361. ~DMA_DESC_COD;
  362. lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
  363. dma_cache_wback((u32)rd, sizeof(*rd));
  364. rd = &lp->rd_ring[lp->rx_next_done];
  365. writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
  366. }
  367. dmas = readl(&lp->rx_dma_regs->dmas);
  368. if (dmas & DMA_STAT_HALT) {
  369. writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
  370. &lp->rx_dma_regs->dmas);
  371. lp->dma_halt_cnt++;
  372. rd->devcs = 0;
  373. skb = lp->rx_skb[lp->rx_next_done];
  374. rd->ca = CPHYSADDR(skb->data);
  375. dma_cache_wback((u32)rd, sizeof(*rd));
  376. korina_chain_rx(lp, rd);
  377. }
  378. return count;
  379. }
  380. static int korina_poll(struct napi_struct *napi, int budget)
  381. {
  382. struct korina_private *lp =
  383. container_of(napi, struct korina_private, napi);
  384. struct net_device *dev = lp->dev;
  385. int work_done;
  386. work_done = korina_rx(dev, budget);
  387. if (work_done < budget) {
  388. napi_complete_done(napi, work_done);
  389. writel(readl(&lp->rx_dma_regs->dmasm) &
  390. ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
  391. &lp->rx_dma_regs->dmasm);
  392. }
  393. return work_done;
  394. }
  395. /*
  396. * Set or clear the multicast filter for this adaptor.
  397. */
  398. static void korina_multicast_list(struct net_device *dev)
  399. {
  400. struct korina_private *lp = netdev_priv(dev);
  401. unsigned long flags;
  402. struct netdev_hw_addr *ha;
  403. u32 recognise = ETH_ARC_AB; /* always accept broadcasts */
  404. /* Set promiscuous mode */
  405. if (dev->flags & IFF_PROMISC)
  406. recognise |= ETH_ARC_PRO;
  407. else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
  408. /* All multicast and broadcast */
  409. recognise |= ETH_ARC_AM;
  410. /* Build the hash table */
  411. if (netdev_mc_count(dev) > 4) {
  412. u16 hash_table[4] = { 0 };
  413. u32 crc;
  414. netdev_for_each_mc_addr(ha, dev) {
  415. crc = ether_crc_le(6, ha->addr);
  416. crc >>= 26;
  417. hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
  418. }
  419. /* Accept filtered multicast */
  420. recognise |= ETH_ARC_AFM;
  421. /* Fill the MAC hash tables with their values */
  422. writel((u32)(hash_table[1] << 16 | hash_table[0]),
  423. &lp->eth_regs->ethhash0);
  424. writel((u32)(hash_table[3] << 16 | hash_table[2]),
  425. &lp->eth_regs->ethhash1);
  426. }
  427. spin_lock_irqsave(&lp->lock, flags);
  428. writel(recognise, &lp->eth_regs->etharc);
  429. spin_unlock_irqrestore(&lp->lock, flags);
  430. }
  431. static void korina_tx(struct net_device *dev)
  432. {
  433. struct korina_private *lp = netdev_priv(dev);
  434. struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
  435. u32 devcs;
  436. u32 dmas;
  437. spin_lock(&lp->lock);
  438. /* Process all desc that are done */
  439. while (IS_DMA_FINISHED(td->control)) {
  440. if (lp->tx_full == 1) {
  441. netif_wake_queue(dev);
  442. lp->tx_full = 0;
  443. }
  444. devcs = lp->td_ring[lp->tx_next_done].devcs;
  445. if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
  446. (ETH_TX_FD | ETH_TX_LD)) {
  447. dev->stats.tx_errors++;
  448. dev->stats.tx_dropped++;
  449. /* Should never happen */
  450. printk(KERN_ERR "%s: split tx ignored\n",
  451. dev->name);
  452. } else if (devcs & ETH_TX_TOK) {
  453. dev->stats.tx_packets++;
  454. dev->stats.tx_bytes +=
  455. lp->tx_skb[lp->tx_next_done]->len;
  456. } else {
  457. dev->stats.tx_errors++;
  458. dev->stats.tx_dropped++;
  459. /* Underflow */
  460. if (devcs & ETH_TX_UND)
  461. dev->stats.tx_fifo_errors++;
  462. /* Oversized frame */
  463. if (devcs & ETH_TX_OF)
  464. dev->stats.tx_aborted_errors++;
  465. /* Excessive deferrals */
  466. if (devcs & ETH_TX_ED)
  467. dev->stats.tx_carrier_errors++;
  468. /* Collisions: medium busy */
  469. if (devcs & ETH_TX_EC)
  470. dev->stats.collisions++;
  471. /* Late collision */
  472. if (devcs & ETH_TX_LC)
  473. dev->stats.tx_window_errors++;
  474. }
  475. /* We must always free the original skb */
  476. if (lp->tx_skb[lp->tx_next_done]) {
  477. dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
  478. lp->tx_skb[lp->tx_next_done] = NULL;
  479. }
  480. lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
  481. lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
  482. lp->td_ring[lp->tx_next_done].link = 0;
  483. lp->td_ring[lp->tx_next_done].ca = 0;
  484. lp->tx_count--;
  485. /* Go on to next transmission */
  486. lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
  487. td = &lp->td_ring[lp->tx_next_done];
  488. }
  489. /* Clear the DMA status register */
  490. dmas = readl(&lp->tx_dma_regs->dmas);
  491. writel(~dmas, &lp->tx_dma_regs->dmas);
  492. writel(readl(&lp->tx_dma_regs->dmasm) &
  493. ~(DMA_STAT_FINI | DMA_STAT_ERR),
  494. &lp->tx_dma_regs->dmasm);
  495. spin_unlock(&lp->lock);
  496. }
  497. static irqreturn_t
  498. korina_tx_dma_interrupt(int irq, void *dev_id)
  499. {
  500. struct net_device *dev = dev_id;
  501. struct korina_private *lp = netdev_priv(dev);
  502. u32 dmas, dmasm;
  503. irqreturn_t retval;
  504. dmas = readl(&lp->tx_dma_regs->dmas);
  505. if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
  506. dmasm = readl(&lp->tx_dma_regs->dmasm);
  507. writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
  508. &lp->tx_dma_regs->dmasm);
  509. korina_tx(dev);
  510. if (lp->tx_chain_status == desc_filled &&
  511. (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
  512. writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
  513. &(lp->tx_dma_regs->dmandptr));
  514. lp->tx_chain_status = desc_empty;
  515. lp->tx_chain_head = lp->tx_chain_tail;
  516. netif_trans_update(dev);
  517. }
  518. if (dmas & DMA_STAT_ERR)
  519. printk(KERN_ERR "%s: DMA error\n", dev->name);
  520. retval = IRQ_HANDLED;
  521. } else
  522. retval = IRQ_NONE;
  523. return retval;
  524. }
  525. static void korina_check_media(struct net_device *dev, unsigned int init_media)
  526. {
  527. struct korina_private *lp = netdev_priv(dev);
  528. mii_check_media(&lp->mii_if, 0, init_media);
  529. if (lp->mii_if.full_duplex)
  530. writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
  531. &lp->eth_regs->ethmac2);
  532. else
  533. writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
  534. &lp->eth_regs->ethmac2);
  535. }
  536. static void korina_poll_media(struct timer_list *t)
  537. {
  538. struct korina_private *lp = from_timer(lp, t, media_check_timer);
  539. struct net_device *dev = lp->dev;
  540. korina_check_media(dev, 0);
  541. mod_timer(&lp->media_check_timer, jiffies + HZ);
  542. }
  543. static void korina_set_carrier(struct mii_if_info *mii)
  544. {
  545. if (mii->force_media) {
  546. /* autoneg is off: Link is always assumed to be up */
  547. if (!netif_carrier_ok(mii->dev))
  548. netif_carrier_on(mii->dev);
  549. } else /* Let MMI library update carrier status */
  550. korina_check_media(mii->dev, 0);
  551. }
  552. static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  553. {
  554. struct korina_private *lp = netdev_priv(dev);
  555. struct mii_ioctl_data *data = if_mii(rq);
  556. int rc;
  557. if (!netif_running(dev))
  558. return -EINVAL;
  559. spin_lock_irq(&lp->lock);
  560. rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
  561. spin_unlock_irq(&lp->lock);
  562. korina_set_carrier(&lp->mii_if);
  563. return rc;
  564. }
  565. /* ethtool helpers */
  566. static void netdev_get_drvinfo(struct net_device *dev,
  567. struct ethtool_drvinfo *info)
  568. {
  569. struct korina_private *lp = netdev_priv(dev);
  570. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  571. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  572. strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info));
  573. }
  574. static int netdev_get_link_ksettings(struct net_device *dev,
  575. struct ethtool_link_ksettings *cmd)
  576. {
  577. struct korina_private *lp = netdev_priv(dev);
  578. spin_lock_irq(&lp->lock);
  579. mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
  580. spin_unlock_irq(&lp->lock);
  581. return 0;
  582. }
  583. static int netdev_set_link_ksettings(struct net_device *dev,
  584. const struct ethtool_link_ksettings *cmd)
  585. {
  586. struct korina_private *lp = netdev_priv(dev);
  587. int rc;
  588. spin_lock_irq(&lp->lock);
  589. rc = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
  590. spin_unlock_irq(&lp->lock);
  591. korina_set_carrier(&lp->mii_if);
  592. return rc;
  593. }
  594. static u32 netdev_get_link(struct net_device *dev)
  595. {
  596. struct korina_private *lp = netdev_priv(dev);
  597. return mii_link_ok(&lp->mii_if);
  598. }
  599. static const struct ethtool_ops netdev_ethtool_ops = {
  600. .get_drvinfo = netdev_get_drvinfo,
  601. .get_link = netdev_get_link,
  602. .get_link_ksettings = netdev_get_link_ksettings,
  603. .set_link_ksettings = netdev_set_link_ksettings,
  604. };
  605. static int korina_alloc_ring(struct net_device *dev)
  606. {
  607. struct korina_private *lp = netdev_priv(dev);
  608. struct sk_buff *skb;
  609. int i;
  610. /* Initialize the transmit descriptors */
  611. for (i = 0; i < KORINA_NUM_TDS; i++) {
  612. lp->td_ring[i].control = DMA_DESC_IOF;
  613. lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
  614. lp->td_ring[i].ca = 0;
  615. lp->td_ring[i].link = 0;
  616. }
  617. lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
  618. lp->tx_full = lp->tx_count = 0;
  619. lp->tx_chain_status = desc_empty;
  620. /* Initialize the receive descriptors */
  621. for (i = 0; i < KORINA_NUM_RDS; i++) {
  622. skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
  623. if (!skb)
  624. return -ENOMEM;
  625. lp->rx_skb[i] = skb;
  626. lp->rd_ring[i].control = DMA_DESC_IOD |
  627. DMA_COUNT(KORINA_RBSIZE);
  628. lp->rd_ring[i].devcs = 0;
  629. lp->rd_ring[i].ca = CPHYSADDR(skb->data);
  630. lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
  631. }
  632. /* loop back receive descriptors, so the last
  633. * descriptor points to the first one */
  634. lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]);
  635. lp->rd_ring[i - 1].control |= DMA_DESC_COD;
  636. lp->rx_next_done = 0;
  637. lp->rx_chain_head = 0;
  638. lp->rx_chain_tail = 0;
  639. lp->rx_chain_status = desc_empty;
  640. return 0;
  641. }
  642. static void korina_free_ring(struct net_device *dev)
  643. {
  644. struct korina_private *lp = netdev_priv(dev);
  645. int i;
  646. for (i = 0; i < KORINA_NUM_RDS; i++) {
  647. lp->rd_ring[i].control = 0;
  648. if (lp->rx_skb[i])
  649. dev_kfree_skb_any(lp->rx_skb[i]);
  650. lp->rx_skb[i] = NULL;
  651. }
  652. for (i = 0; i < KORINA_NUM_TDS; i++) {
  653. lp->td_ring[i].control = 0;
  654. if (lp->tx_skb[i])
  655. dev_kfree_skb_any(lp->tx_skb[i]);
  656. lp->tx_skb[i] = NULL;
  657. }
  658. }
  659. /*
  660. * Initialize the RC32434 ethernet controller.
  661. */
  662. static int korina_init(struct net_device *dev)
  663. {
  664. struct korina_private *lp = netdev_priv(dev);
  665. /* Disable DMA */
  666. korina_abort_tx(dev);
  667. korina_abort_rx(dev);
  668. /* reset ethernet logic */
  669. writel(0, &lp->eth_regs->ethintfc);
  670. while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
  671. netif_trans_update(dev);
  672. /* Enable Ethernet Interface */
  673. writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
  674. /* Allocate rings */
  675. if (korina_alloc_ring(dev)) {
  676. printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
  677. korina_free_ring(dev);
  678. return -ENOMEM;
  679. }
  680. writel(0, &lp->rx_dma_regs->dmas);
  681. /* Start Rx DMA */
  682. korina_start_rx(lp, &lp->rd_ring[0]);
  683. writel(readl(&lp->tx_dma_regs->dmasm) &
  684. ~(DMA_STAT_FINI | DMA_STAT_ERR),
  685. &lp->tx_dma_regs->dmasm);
  686. writel(readl(&lp->rx_dma_regs->dmasm) &
  687. ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
  688. &lp->rx_dma_regs->dmasm);
  689. /* Accept only packets destined for this Ethernet device address */
  690. writel(ETH_ARC_AB, &lp->eth_regs->etharc);
  691. /* Set all Ether station address registers to their initial values */
  692. writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
  693. writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
  694. writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
  695. writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
  696. writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
  697. writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
  698. writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
  699. writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
  700. /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
  701. writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
  702. &lp->eth_regs->ethmac2);
  703. /* Back to back inter-packet-gap */
  704. writel(0x15, &lp->eth_regs->ethipgt);
  705. /* Non - Back to back inter-packet-gap */
  706. writel(0x12, &lp->eth_regs->ethipgr);
  707. /* Management Clock Prescaler Divisor
  708. * Clock independent setting */
  709. writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
  710. &lp->eth_regs->ethmcp);
  711. /* don't transmit until fifo contains 48b */
  712. writel(48, &lp->eth_regs->ethfifott);
  713. writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
  714. napi_enable(&lp->napi);
  715. netif_start_queue(dev);
  716. return 0;
  717. }
  718. /*
  719. * Restart the RC32434 ethernet controller.
  720. */
  721. static void korina_restart_task(struct work_struct *work)
  722. {
  723. struct korina_private *lp = container_of(work,
  724. struct korina_private, restart_task);
  725. struct net_device *dev = lp->dev;
  726. /*
  727. * Disable interrupts
  728. */
  729. disable_irq(lp->rx_irq);
  730. disable_irq(lp->tx_irq);
  731. writel(readl(&lp->tx_dma_regs->dmasm) |
  732. DMA_STAT_FINI | DMA_STAT_ERR,
  733. &lp->tx_dma_regs->dmasm);
  734. writel(readl(&lp->rx_dma_regs->dmasm) |
  735. DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
  736. &lp->rx_dma_regs->dmasm);
  737. napi_disable(&lp->napi);
  738. korina_free_ring(dev);
  739. if (korina_init(dev) < 0) {
  740. printk(KERN_ERR "%s: cannot restart device\n", dev->name);
  741. return;
  742. }
  743. korina_multicast_list(dev);
  744. enable_irq(lp->tx_irq);
  745. enable_irq(lp->rx_irq);
  746. }
  747. static void korina_tx_timeout(struct net_device *dev)
  748. {
  749. struct korina_private *lp = netdev_priv(dev);
  750. schedule_work(&lp->restart_task);
  751. }
  752. #ifdef CONFIG_NET_POLL_CONTROLLER
  753. static void korina_poll_controller(struct net_device *dev)
  754. {
  755. disable_irq(dev->irq);
  756. korina_tx_dma_interrupt(dev->irq, dev);
  757. enable_irq(dev->irq);
  758. }
  759. #endif
  760. static int korina_open(struct net_device *dev)
  761. {
  762. struct korina_private *lp = netdev_priv(dev);
  763. int ret;
  764. /* Initialize */
  765. ret = korina_init(dev);
  766. if (ret < 0) {
  767. printk(KERN_ERR "%s: cannot open device\n", dev->name);
  768. goto out;
  769. }
  770. /* Install the interrupt handler
  771. * that handles the Done Finished */
  772. ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
  773. 0, "Korina ethernet Rx", dev);
  774. if (ret < 0) {
  775. printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
  776. dev->name, lp->rx_irq);
  777. goto err_release;
  778. }
  779. ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
  780. 0, "Korina ethernet Tx", dev);
  781. if (ret < 0) {
  782. printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
  783. dev->name, lp->tx_irq);
  784. goto err_free_rx_irq;
  785. }
  786. mod_timer(&lp->media_check_timer, jiffies + 1);
  787. out:
  788. return ret;
  789. err_free_rx_irq:
  790. free_irq(lp->rx_irq, dev);
  791. err_release:
  792. korina_free_ring(dev);
  793. goto out;
  794. }
  795. static int korina_close(struct net_device *dev)
  796. {
  797. struct korina_private *lp = netdev_priv(dev);
  798. u32 tmp;
  799. del_timer(&lp->media_check_timer);
  800. /* Disable interrupts */
  801. disable_irq(lp->rx_irq);
  802. disable_irq(lp->tx_irq);
  803. korina_abort_tx(dev);
  804. tmp = readl(&lp->tx_dma_regs->dmasm);
  805. tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
  806. writel(tmp, &lp->tx_dma_regs->dmasm);
  807. korina_abort_rx(dev);
  808. tmp = readl(&lp->rx_dma_regs->dmasm);
  809. tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
  810. writel(tmp, &lp->rx_dma_regs->dmasm);
  811. napi_disable(&lp->napi);
  812. cancel_work_sync(&lp->restart_task);
  813. korina_free_ring(dev);
  814. free_irq(lp->rx_irq, dev);
  815. free_irq(lp->tx_irq, dev);
  816. return 0;
  817. }
  818. static const struct net_device_ops korina_netdev_ops = {
  819. .ndo_open = korina_open,
  820. .ndo_stop = korina_close,
  821. .ndo_start_xmit = korina_send_packet,
  822. .ndo_set_rx_mode = korina_multicast_list,
  823. .ndo_tx_timeout = korina_tx_timeout,
  824. .ndo_do_ioctl = korina_ioctl,
  825. .ndo_validate_addr = eth_validate_addr,
  826. .ndo_set_mac_address = eth_mac_addr,
  827. #ifdef CONFIG_NET_POLL_CONTROLLER
  828. .ndo_poll_controller = korina_poll_controller,
  829. #endif
  830. };
  831. static int korina_probe(struct platform_device *pdev)
  832. {
  833. struct korina_device *bif = platform_get_drvdata(pdev);
  834. struct korina_private *lp;
  835. struct net_device *dev;
  836. struct resource *r;
  837. int rc;
  838. dev = alloc_etherdev(sizeof(struct korina_private));
  839. if (!dev)
  840. return -ENOMEM;
  841. SET_NETDEV_DEV(dev, &pdev->dev);
  842. lp = netdev_priv(dev);
  843. bif->dev = dev;
  844. memcpy(dev->dev_addr, bif->mac, ETH_ALEN);
  845. lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
  846. lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
  847. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
  848. dev->base_addr = r->start;
  849. lp->eth_regs = ioremap_nocache(r->start, resource_size(r));
  850. if (!lp->eth_regs) {
  851. printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
  852. rc = -ENXIO;
  853. goto probe_err_out;
  854. }
  855. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
  856. lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r));
  857. if (!lp->rx_dma_regs) {
  858. printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
  859. rc = -ENXIO;
  860. goto probe_err_dma_rx;
  861. }
  862. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
  863. lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r));
  864. if (!lp->tx_dma_regs) {
  865. printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
  866. rc = -ENXIO;
  867. goto probe_err_dma_tx;
  868. }
  869. lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
  870. if (!lp->td_ring) {
  871. rc = -ENXIO;
  872. goto probe_err_td_ring;
  873. }
  874. dma_cache_inv((unsigned long)(lp->td_ring),
  875. TD_RING_SIZE + RD_RING_SIZE);
  876. /* now convert TD_RING pointer to KSEG1 */
  877. lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring);
  878. lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS];
  879. spin_lock_init(&lp->lock);
  880. /* just use the rx dma irq */
  881. dev->irq = lp->rx_irq;
  882. lp->dev = dev;
  883. dev->netdev_ops = &korina_netdev_ops;
  884. dev->ethtool_ops = &netdev_ethtool_ops;
  885. dev->watchdog_timeo = TX_TIMEOUT;
  886. netif_napi_add(dev, &lp->napi, korina_poll, NAPI_POLL_WEIGHT);
  887. lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
  888. lp->mii_if.dev = dev;
  889. lp->mii_if.mdio_read = mdio_read;
  890. lp->mii_if.mdio_write = mdio_write;
  891. lp->mii_if.phy_id = lp->phy_addr;
  892. lp->mii_if.phy_id_mask = 0x1f;
  893. lp->mii_if.reg_num_mask = 0x1f;
  894. rc = register_netdev(dev);
  895. if (rc < 0) {
  896. printk(KERN_ERR DRV_NAME
  897. ": cannot register net device: %d\n", rc);
  898. goto probe_err_register;
  899. }
  900. timer_setup(&lp->media_check_timer, korina_poll_media, 0);
  901. INIT_WORK(&lp->restart_task, korina_restart_task);
  902. printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
  903. dev->name);
  904. out:
  905. return rc;
  906. probe_err_register:
  907. kfree(lp->td_ring);
  908. probe_err_td_ring:
  909. iounmap(lp->tx_dma_regs);
  910. probe_err_dma_tx:
  911. iounmap(lp->rx_dma_regs);
  912. probe_err_dma_rx:
  913. iounmap(lp->eth_regs);
  914. probe_err_out:
  915. free_netdev(dev);
  916. goto out;
  917. }
  918. static int korina_remove(struct platform_device *pdev)
  919. {
  920. struct korina_device *bif = platform_get_drvdata(pdev);
  921. struct korina_private *lp = netdev_priv(bif->dev);
  922. iounmap(lp->eth_regs);
  923. iounmap(lp->rx_dma_regs);
  924. iounmap(lp->tx_dma_regs);
  925. unregister_netdev(bif->dev);
  926. free_netdev(bif->dev);
  927. return 0;
  928. }
  929. static struct platform_driver korina_driver = {
  930. .driver.name = "korina",
  931. .probe = korina_probe,
  932. .remove = korina_remove,
  933. };
  934. module_platform_driver(korina_driver);
  935. MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
  936. MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  937. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  938. MODULE_AUTHOR("Roman Yeryomin <roman@advem.lv>");
  939. MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
  940. MODULE_LICENSE("GPL");