stnic.c 7.3 KB

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