stnic.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, (S_IRUSR|S_IRGRP|S_IROTH));
  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. int i, err;
  88. struct ei_device *ei_local;
  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. for (i = 0; i < ETH_ALEN; i++)
  100. dev->dev_addr[i] = stnic_eadr[i];
  101. /* Set the base address to point to the NIC, not the "real" base! */
  102. dev->base_addr = 0x1000;
  103. dev->irq = IRQ_STNIC;
  104. dev->netdev_ops = &ei_netdev_ops;
  105. /* Snarf the interrupt now. There's no point in waiting since we cannot
  106. share and the board will usually be enabled. */
  107. err = request_irq (dev->irq, ei_interrupt, 0, DRV_NAME, dev);
  108. if (err) {
  109. netdev_emerg(dev, " unable to get IRQ %d.\n", dev->irq);
  110. free_netdev(dev);
  111. return err;
  112. }
  113. ei_status.name = dev->name;
  114. ei_status.word16 = 1;
  115. #ifdef __LITTLE_ENDIAN__
  116. ei_status.bigendian = 0;
  117. #else
  118. ei_status.bigendian = 1;
  119. #endif
  120. ei_status.tx_start_page = START_PG;
  121. ei_status.rx_start_page = START_PG + TX_PAGES;
  122. ei_status.stop_page = STOP_PG;
  123. ei_status.reset_8390 = &stnic_reset;
  124. ei_status.get_8390_hdr = &stnic_get_hdr;
  125. ei_status.block_input = &stnic_block_input;
  126. ei_status.block_output = &stnic_block_output;
  127. stnic_init (dev);
  128. ei_local = netdev_priv(dev);
  129. ei_local->msg_enable = stnic_msg_enable;
  130. err = register_netdev(dev);
  131. if (err) {
  132. free_irq(dev->irq, dev);
  133. free_netdev(dev);
  134. return err;
  135. }
  136. stnic_dev = dev;
  137. netdev_info(dev, "NS ST-NIC 83902A\n");
  138. return 0;
  139. }
  140. static void
  141. stnic_reset (struct net_device *dev)
  142. {
  143. struct ei_device *ei_local = netdev_priv(dev);
  144. *(vhalf *) PA_83902_RST = 0;
  145. udelay (5);
  146. netif_warn(ei_local, hw, dev, "8390 reset done (%ld).\n", jiffies);
  147. *(vhalf *) PA_83902_RST = ~0;
  148. udelay (5);
  149. }
  150. static void
  151. stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
  152. int ring_page)
  153. {
  154. struct ei_device *ei_local = netdev_priv(dev);
  155. half buf[2];
  156. STNIC_WRITE (PG0_RSAR0, 0);
  157. STNIC_WRITE (PG0_RSAR1, ring_page);
  158. STNIC_WRITE (PG0_RBCR0, 4);
  159. STNIC_WRITE (PG0_RBCR1, 0);
  160. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  161. buf[0] = *(vhalf *) PA_83902_IF;
  162. STNIC_DELAY ();
  163. buf[1] = *(vhalf *) PA_83902_IF;
  164. STNIC_DELAY ();
  165. hdr->next = buf[0] >> 8;
  166. hdr->status = buf[0] & 0xff;
  167. #ifdef __LITTLE_ENDIAN__
  168. hdr->count = buf[1];
  169. #else
  170. hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
  171. #endif
  172. netif_dbg(ei_local, probe, dev, "ring %x status %02x next %02x count %04x.\n",
  173. ring_page, hdr->status, hdr->next, hdr->count);
  174. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  175. }
  176. /* Block input and output, similar to the Crynwr packet driver. If you are
  177. porting to a new ethercard look at the packet driver source for hints.
  178. The HP LAN doesn't use shared memory -- we put the packet
  179. out through the "remote DMA" dataport. */
  180. static void
  181. stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
  182. int offset)
  183. {
  184. char *buf = skb->data;
  185. half val;
  186. STNIC_WRITE (PG0_RSAR0, offset & 0xff);
  187. STNIC_WRITE (PG0_RSAR1, offset >> 8);
  188. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  189. STNIC_WRITE (PG0_RBCR1, length >> 8);
  190. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  191. if (length & 1)
  192. length++;
  193. while (length > 0)
  194. {
  195. val = *(vhalf *) PA_83902_IF;
  196. #ifdef __LITTLE_ENDIAN__
  197. *buf++ = val & 0xff;
  198. *buf++ = val >> 8;
  199. #else
  200. *buf++ = val >> 8;
  201. *buf++ = val & 0xff;
  202. #endif
  203. STNIC_DELAY ();
  204. length -= sizeof (half);
  205. }
  206. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  207. }
  208. static void
  209. stnic_block_output (struct net_device *dev, int length,
  210. const unsigned char *buf, int output_page)
  211. {
  212. STNIC_WRITE (PG0_RBCR0, 1); /* Write non-zero value */
  213. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  214. STNIC_DELAY ();
  215. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  216. STNIC_WRITE (PG0_RBCR1, length >> 8);
  217. STNIC_WRITE (PG0_RSAR0, 0);
  218. STNIC_WRITE (PG0_RSAR1, output_page);
  219. STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
  220. if (length & 1)
  221. length++;
  222. while (length > 0)
  223. {
  224. #ifdef __LITTLE_ENDIAN__
  225. *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
  226. #else
  227. *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
  228. #endif
  229. STNIC_DELAY ();
  230. buf += sizeof (half);
  231. length -= sizeof (half);
  232. }
  233. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  234. }
  235. /* This function resets the STNIC if something screws up. */
  236. static void
  237. stnic_init (struct net_device *dev)
  238. {
  239. stnic_reset (dev);
  240. NS8390_init (dev, 0);
  241. }
  242. static void __exit stnic_cleanup(void)
  243. {
  244. unregister_netdev(stnic_dev);
  245. free_irq(stnic_dev->irq, stnic_dev);
  246. free_netdev(stnic_dev);
  247. }
  248. module_init(stnic_probe);
  249. module_exit(stnic_cleanup);
  250. MODULE_LICENSE("GPL");