stnic.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* stnic.c : A SH7750 specific part of driver for NS DP83902A ST-NIC.
  3. *
  4. * Copyright (C) 1999 kaz Kojima
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/ioport.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <asm/io.h>
  16. #include <mach-se/mach/se.h>
  17. #include <asm/machvec.h>
  18. #ifdef CONFIG_SH_STANDARD_BIOS
  19. #include <asm/sh_bios.h>
  20. #endif
  21. #include "8390.h"
  22. #define DRV_NAME "stnic"
  23. #define byte unsigned char
  24. #define half unsigned short
  25. #define word unsigned int
  26. #define vbyte volatile unsigned char
  27. #define vhalf volatile unsigned short
  28. #define vword volatile unsigned int
  29. #define STNIC_RUN 0x01 /* 1 == Run, 0 == reset. */
  30. #define START_PG 0 /* First page of TX buffer */
  31. #define STOP_PG 128 /* Last page +1 of RX ring */
  32. /* Alias */
  33. #define STNIC_CR E8390_CMD
  34. #define PG0_RSAR0 EN0_RSARLO
  35. #define PG0_RSAR1 EN0_RSARHI
  36. #define PG0_RBCR0 EN0_RCNTLO
  37. #define PG0_RBCR1 EN0_RCNTHI
  38. #define CR_RRD E8390_RREAD
  39. #define CR_RWR E8390_RWRITE
  40. #define CR_PG0 E8390_PAGE0
  41. #define CR_STA E8390_START
  42. #define CR_RDMA E8390_NODMA
  43. /* FIXME! YOU MUST SET YOUR OWN ETHER ADDRESS. */
  44. static byte stnic_eadr[6] =
  45. {0x00, 0xc0, 0x6e, 0x00, 0x00, 0x07};
  46. static struct net_device *stnic_dev;
  47. static void stnic_reset (struct net_device *dev);
  48. static void stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
  49. int ring_page);
  50. static void stnic_block_input (struct net_device *dev, int count,
  51. struct sk_buff *skb , int ring_offset);
  52. static void stnic_block_output (struct net_device *dev, int count,
  53. const unsigned char *buf, int start_page);
  54. static void stnic_init (struct net_device *dev);
  55. static u32 stnic_msg_enable;
  56. module_param_named(msg_enable, stnic_msg_enable, uint, 0444);
  57. MODULE_PARM_DESC(msg_enable, "Debug message level (see linux/netdevice.h for bitmap)");
  58. /* SH7750 specific read/write io. */
  59. static inline void
  60. STNIC_DELAY (void)
  61. {
  62. vword trash;
  63. trash = *(vword *) 0xa0000000;
  64. trash = *(vword *) 0xa0000000;
  65. trash = *(vword *) 0xa0000000;
  66. }
  67. static inline byte
  68. STNIC_READ (int reg)
  69. {
  70. byte val;
  71. val = (*(vhalf *) (PA_83902 + ((reg) << 1)) >> 8) & 0xff;
  72. STNIC_DELAY ();
  73. return val;
  74. }
  75. static inline void
  76. STNIC_WRITE (int reg, byte val)
  77. {
  78. *(vhalf *) (PA_83902 + ((reg) << 1)) = ((half) (val) << 8);
  79. STNIC_DELAY ();
  80. }
  81. static int __init stnic_probe(void)
  82. {
  83. struct net_device *dev;
  84. struct ei_device *ei_local;
  85. int err;
  86. /* If we are not running on a SolutionEngine, give up now */
  87. if (! MACH_SE)
  88. return -ENODEV;
  89. /* New style probing API */
  90. dev = alloc_ei_netdev();
  91. if (!dev)
  92. return -ENOMEM;
  93. #ifdef CONFIG_SH_STANDARD_BIOS
  94. sh_bios_get_node_addr (stnic_eadr);
  95. #endif
  96. eth_hw_addr_set(dev, stnic_eadr);
  97. /* Set the base address to point to the NIC, not the "real" base! */
  98. dev->base_addr = 0x1000;
  99. dev->irq = IRQ_STNIC;
  100. dev->netdev_ops = &ei_netdev_ops;
  101. /* Snarf the interrupt now. There's no point in waiting since we cannot
  102. share and the board will usually be enabled. */
  103. err = request_irq (dev->irq, ei_interrupt, 0, DRV_NAME, dev);
  104. if (err) {
  105. netdev_emerg(dev, " unable to get IRQ %d.\n", dev->irq);
  106. free_netdev(dev);
  107. return err;
  108. }
  109. ei_status.name = dev->name;
  110. ei_status.word16 = 1;
  111. #ifdef __LITTLE_ENDIAN__
  112. ei_status.bigendian = 0;
  113. #else
  114. ei_status.bigendian = 1;
  115. #endif
  116. ei_status.tx_start_page = START_PG;
  117. ei_status.rx_start_page = START_PG + TX_PAGES;
  118. ei_status.stop_page = STOP_PG;
  119. ei_status.reset_8390 = &stnic_reset;
  120. ei_status.get_8390_hdr = &stnic_get_hdr;
  121. ei_status.block_input = &stnic_block_input;
  122. ei_status.block_output = &stnic_block_output;
  123. stnic_init (dev);
  124. ei_local = netdev_priv(dev);
  125. ei_local->msg_enable = stnic_msg_enable;
  126. err = register_netdev(dev);
  127. if (err) {
  128. free_irq(dev->irq, dev);
  129. free_netdev(dev);
  130. return err;
  131. }
  132. stnic_dev = dev;
  133. netdev_info(dev, "NS ST-NIC 83902A\n");
  134. return 0;
  135. }
  136. static void
  137. stnic_reset (struct net_device *dev)
  138. {
  139. struct ei_device *ei_local = netdev_priv(dev);
  140. *(vhalf *) PA_83902_RST = 0;
  141. udelay (5);
  142. netif_warn(ei_local, hw, dev, "8390 reset done (%ld).\n", jiffies);
  143. *(vhalf *) PA_83902_RST = ~0;
  144. udelay (5);
  145. }
  146. static void
  147. stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
  148. int ring_page)
  149. {
  150. struct ei_device *ei_local = netdev_priv(dev);
  151. half buf[2];
  152. STNIC_WRITE (PG0_RSAR0, 0);
  153. STNIC_WRITE (PG0_RSAR1, ring_page);
  154. STNIC_WRITE (PG0_RBCR0, 4);
  155. STNIC_WRITE (PG0_RBCR1, 0);
  156. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  157. buf[0] = *(vhalf *) PA_83902_IF;
  158. STNIC_DELAY ();
  159. buf[1] = *(vhalf *) PA_83902_IF;
  160. STNIC_DELAY ();
  161. hdr->next = buf[0] >> 8;
  162. hdr->status = buf[0] & 0xff;
  163. #ifdef __LITTLE_ENDIAN__
  164. hdr->count = buf[1];
  165. #else
  166. hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
  167. #endif
  168. netif_dbg(ei_local, probe, dev, "ring %x status %02x next %02x count %04x.\n",
  169. ring_page, hdr->status, hdr->next, hdr->count);
  170. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  171. }
  172. /* Block input and output, similar to the Crynwr packet driver. If you are
  173. porting to a new ethercard look at the packet driver source for hints.
  174. The HP LAN doesn't use shared memory -- we put the packet
  175. out through the "remote DMA" dataport. */
  176. static void
  177. stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
  178. int offset)
  179. {
  180. char *buf = skb->data;
  181. half val;
  182. STNIC_WRITE (PG0_RSAR0, offset & 0xff);
  183. STNIC_WRITE (PG0_RSAR1, offset >> 8);
  184. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  185. STNIC_WRITE (PG0_RBCR1, length >> 8);
  186. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  187. if (length & 1)
  188. length++;
  189. while (length > 0)
  190. {
  191. val = *(vhalf *) PA_83902_IF;
  192. #ifdef __LITTLE_ENDIAN__
  193. *buf++ = val & 0xff;
  194. *buf++ = val >> 8;
  195. #else
  196. *buf++ = val >> 8;
  197. *buf++ = val & 0xff;
  198. #endif
  199. STNIC_DELAY ();
  200. length -= sizeof (half);
  201. }
  202. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  203. }
  204. static void
  205. stnic_block_output (struct net_device *dev, int length,
  206. const unsigned char *buf, int output_page)
  207. {
  208. STNIC_WRITE (PG0_RBCR0, 1); /* Write non-zero value */
  209. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  210. STNIC_DELAY ();
  211. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  212. STNIC_WRITE (PG0_RBCR1, length >> 8);
  213. STNIC_WRITE (PG0_RSAR0, 0);
  214. STNIC_WRITE (PG0_RSAR1, output_page);
  215. STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
  216. if (length & 1)
  217. length++;
  218. while (length > 0)
  219. {
  220. #ifdef __LITTLE_ENDIAN__
  221. *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
  222. #else
  223. *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
  224. #endif
  225. STNIC_DELAY ();
  226. buf += sizeof (half);
  227. length -= sizeof (half);
  228. }
  229. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  230. }
  231. /* This function resets the STNIC if something screws up. */
  232. static void
  233. stnic_init (struct net_device *dev)
  234. {
  235. stnic_reset (dev);
  236. NS8390_init (dev, 0);
  237. }
  238. static void __exit stnic_cleanup(void)
  239. {
  240. unregister_netdev(stnic_dev);
  241. free_irq(stnic_dev->irq, stnic_dev);
  242. free_netdev(stnic_dev);
  243. }
  244. module_init(stnic_probe);
  245. module_exit(stnic_cleanup);
  246. MODULE_DESCRIPTION("National Semiconductor DP83902AV ethernet driver");
  247. MODULE_LICENSE("GPL");