stnic.c 7.0 KB

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