stnic.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /* 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. int i, err;
  85. /* If we are not running on a SolutionEngine, give up now */
  86. if (! MACH_SE)
  87. return -ENODEV;
  88. /* New style probing API */
  89. dev = alloc_ei_netdev();
  90. if (!dev)
  91. return -ENOMEM;
  92. #ifdef CONFIG_SH_STANDARD_BIOS
  93. sh_bios_get_node_addr (stnic_eadr);
  94. #endif
  95. for (i = 0; i < ETH_ALEN; i++)
  96. dev->dev_addr[i] = stnic_eadr[i];
  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. printk (KERN_EMERG " 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. err = register_netdev(dev);
  125. if (err) {
  126. free_irq(dev->irq, dev);
  127. free_netdev(dev);
  128. return err;
  129. }
  130. stnic_dev = dev;
  131. printk (KERN_INFO "NS ST-NIC 83902A\n");
  132. return 0;
  133. }
  134. static void
  135. stnic_reset (struct net_device *dev)
  136. {
  137. *(vhalf *) PA_83902_RST = 0;
  138. udelay (5);
  139. if (ei_debug > 1)
  140. printk (KERN_WARNING "8390 reset done (%ld).\n", jiffies);
  141. *(vhalf *) PA_83902_RST = ~0;
  142. udelay (5);
  143. }
  144. static void
  145. stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
  146. int ring_page)
  147. {
  148. half buf[2];
  149. STNIC_WRITE (PG0_RSAR0, 0);
  150. STNIC_WRITE (PG0_RSAR1, ring_page);
  151. STNIC_WRITE (PG0_RBCR0, 4);
  152. STNIC_WRITE (PG0_RBCR1, 0);
  153. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  154. buf[0] = *(vhalf *) PA_83902_IF;
  155. STNIC_DELAY ();
  156. buf[1] = *(vhalf *) PA_83902_IF;
  157. STNIC_DELAY ();
  158. hdr->next = buf[0] >> 8;
  159. hdr->status = buf[0] & 0xff;
  160. #ifdef __LITTLE_ENDIAN__
  161. hdr->count = buf[1];
  162. #else
  163. hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
  164. #endif
  165. if (ei_debug > 1)
  166. printk (KERN_DEBUG "ring %x status %02x next %02x count %04x.\n",
  167. ring_page, hdr->status, hdr->next, hdr->count);
  168. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  169. }
  170. /* Block input and output, similar to the Crynwr packet driver. If you are
  171. porting to a new ethercard look at the packet driver source for hints.
  172. The HP LAN doesn't use shared memory -- we put the packet
  173. out through the "remote DMA" dataport. */
  174. static void
  175. stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
  176. int offset)
  177. {
  178. char *buf = skb->data;
  179. half val;
  180. STNIC_WRITE (PG0_RSAR0, offset & 0xff);
  181. STNIC_WRITE (PG0_RSAR1, offset >> 8);
  182. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  183. STNIC_WRITE (PG0_RBCR1, length >> 8);
  184. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  185. if (length & 1)
  186. length++;
  187. while (length > 0)
  188. {
  189. val = *(vhalf *) PA_83902_IF;
  190. #ifdef __LITTLE_ENDIAN__
  191. *buf++ = val & 0xff;
  192. *buf++ = val >> 8;
  193. #else
  194. *buf++ = val >> 8;
  195. *buf++ = val & 0xff;
  196. #endif
  197. STNIC_DELAY ();
  198. length -= sizeof (half);
  199. }
  200. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  201. }
  202. static void
  203. stnic_block_output (struct net_device *dev, int length,
  204. const unsigned char *buf, int output_page)
  205. {
  206. STNIC_WRITE (PG0_RBCR0, 1); /* Write non-zero value */
  207. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  208. STNIC_DELAY ();
  209. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  210. STNIC_WRITE (PG0_RBCR1, length >> 8);
  211. STNIC_WRITE (PG0_RSAR0, 0);
  212. STNIC_WRITE (PG0_RSAR1, output_page);
  213. STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
  214. if (length & 1)
  215. length++;
  216. while (length > 0)
  217. {
  218. #ifdef __LITTLE_ENDIAN__
  219. *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
  220. #else
  221. *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
  222. #endif
  223. STNIC_DELAY ();
  224. buf += sizeof (half);
  225. length -= sizeof (half);
  226. }
  227. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  228. }
  229. /* This function resets the STNIC if something screws up. */
  230. static void
  231. stnic_init (struct net_device *dev)
  232. {
  233. stnic_reset (dev);
  234. NS8390_init (dev, 0);
  235. }
  236. static void __exit stnic_cleanup(void)
  237. {
  238. unregister_netdev(stnic_dev);
  239. free_irq(stnic_dev->irq, stnic_dev);
  240. free_netdev(stnic_dev);
  241. }
  242. module_init(stnic_probe);
  243. module_exit(stnic_cleanup);
  244. MODULE_LICENSE("GPL");