stnic.c 7.5 KB

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