stnic.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <asm/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 int stnic_open (struct net_device *dev);
  52. static int stnic_close (struct net_device *dev);
  53. static void stnic_reset (struct net_device *dev);
  54. static void stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
  55. int ring_page);
  56. static void stnic_block_input (struct net_device *dev, int count,
  57. struct sk_buff *skb , int ring_offset);
  58. static void stnic_block_output (struct net_device *dev, int count,
  59. const unsigned char *buf, int start_page);
  60. static void stnic_init (struct net_device *dev);
  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. /* If we are not running on a SolutionEngine, give up now */
  89. if (! MACH_SE)
  90. return -ENODEV;
  91. /* New style probing API */
  92. dev = alloc_ei_netdev();
  93. if (!dev)
  94. return -ENOMEM;
  95. SET_MODULE_OWNER(dev);
  96. #ifdef CONFIG_SH_STANDARD_BIOS
  97. sh_bios_get_node_addr (stnic_eadr);
  98. #endif
  99. for (i = 0; i < ETHER_ADDR_LEN; 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->open = &stnic_open;
  105. dev->stop = &stnic_close;
  106. #ifdef CONFIG_NET_POLL_CONTROLLER
  107. dev->poll_controller = ei_poll;
  108. #endif
  109. /* Snarf the interrupt now. There's no point in waiting since we cannot
  110. share and the board will usually be enabled. */
  111. err = request_irq (dev->irq, ei_interrupt, 0, DRV_NAME, dev);
  112. if (err) {
  113. printk (KERN_EMERG " unable to get IRQ %d.\n", dev->irq);
  114. free_netdev(dev);
  115. return err;
  116. }
  117. ei_status.name = dev->name;
  118. ei_status.word16 = 1;
  119. #ifdef __LITTLE_ENDIAN__
  120. ei_status.bigendian = 0;
  121. #else
  122. ei_status.bigendian = 1;
  123. #endif
  124. ei_status.tx_start_page = START_PG;
  125. ei_status.rx_start_page = START_PG + TX_PAGES;
  126. ei_status.stop_page = STOP_PG;
  127. ei_status.reset_8390 = &stnic_reset;
  128. ei_status.get_8390_hdr = &stnic_get_hdr;
  129. ei_status.block_input = &stnic_block_input;
  130. ei_status.block_output = &stnic_block_output;
  131. stnic_init (dev);
  132. err = register_netdev(dev);
  133. if (err) {
  134. free_irq(dev->irq, dev);
  135. free_netdev(dev);
  136. return err;
  137. }
  138. stnic_dev = dev;
  139. printk (KERN_INFO "NS ST-NIC 83902A\n");
  140. return 0;
  141. }
  142. static int
  143. stnic_open (struct net_device *dev)
  144. {
  145. #if 0
  146. printk (KERN_DEBUG "stnic open\n");
  147. #endif
  148. ei_open (dev);
  149. return 0;
  150. }
  151. static int
  152. stnic_close (struct net_device *dev)
  153. {
  154. ei_close (dev);
  155. return 0;
  156. }
  157. static void
  158. stnic_reset (struct net_device *dev)
  159. {
  160. *(vhalf *) PA_83902_RST = 0;
  161. udelay (5);
  162. if (ei_debug > 1)
  163. printk (KERN_WARNING "8390 reset done (%ld).\n", jiffies);
  164. *(vhalf *) PA_83902_RST = ~0;
  165. udelay (5);
  166. }
  167. static void
  168. stnic_get_hdr (struct net_device *dev, struct e8390_pkt_hdr *hdr,
  169. int ring_page)
  170. {
  171. half buf[2];
  172. STNIC_WRITE (PG0_RSAR0, 0);
  173. STNIC_WRITE (PG0_RSAR1, ring_page);
  174. STNIC_WRITE (PG0_RBCR0, 4);
  175. STNIC_WRITE (PG0_RBCR1, 0);
  176. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  177. buf[0] = *(vhalf *) PA_83902_IF;
  178. STNIC_DELAY ();
  179. buf[1] = *(vhalf *) PA_83902_IF;
  180. STNIC_DELAY ();
  181. hdr->next = buf[0] >> 8;
  182. hdr->status = buf[0] & 0xff;
  183. #ifdef __LITTLE_ENDIAN__
  184. hdr->count = buf[1];
  185. #else
  186. hdr->count = ((buf[1] >> 8) & 0xff) | (buf[1] << 8);
  187. #endif
  188. if (ei_debug > 1)
  189. printk (KERN_DEBUG "ring %x status %02x next %02x count %04x.\n",
  190. ring_page, hdr->status, hdr->next, hdr->count);
  191. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  192. }
  193. /* Block input and output, similar to the Crynwr packet driver. If you are
  194. porting to a new ethercard look at the packet driver source for hints.
  195. The HP LAN doesn't use shared memory -- we put the packet
  196. out through the "remote DMA" dataport. */
  197. static void
  198. stnic_block_input (struct net_device *dev, int length, struct sk_buff *skb,
  199. int offset)
  200. {
  201. char *buf = skb->data;
  202. half val;
  203. STNIC_WRITE (PG0_RSAR0, offset & 0xff);
  204. STNIC_WRITE (PG0_RSAR1, offset >> 8);
  205. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  206. STNIC_WRITE (PG0_RBCR1, length >> 8);
  207. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  208. if (length & 1)
  209. length++;
  210. while (length > 0)
  211. {
  212. val = *(vhalf *) PA_83902_IF;
  213. #ifdef __LITTLE_ENDIAN__
  214. *buf++ = val & 0xff;
  215. *buf++ = val >> 8;
  216. #else
  217. *buf++ = val >> 8;
  218. *buf++ = val & 0xff;
  219. #endif
  220. STNIC_DELAY ();
  221. length -= sizeof (half);
  222. }
  223. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  224. }
  225. static void
  226. stnic_block_output (struct net_device *dev, int length,
  227. const unsigned char *buf, int output_page)
  228. {
  229. STNIC_WRITE (PG0_RBCR0, 1); /* Write non-zero value */
  230. STNIC_WRITE (STNIC_CR, CR_RRD | CR_PG0 | CR_STA);
  231. STNIC_DELAY ();
  232. STNIC_WRITE (PG0_RBCR0, length & 0xff);
  233. STNIC_WRITE (PG0_RBCR1, length >> 8);
  234. STNIC_WRITE (PG0_RSAR0, 0);
  235. STNIC_WRITE (PG0_RSAR1, output_page);
  236. STNIC_WRITE (STNIC_CR, CR_RWR | CR_PG0 | CR_STA);
  237. if (length & 1)
  238. length++;
  239. while (length > 0)
  240. {
  241. #ifdef __LITTLE_ENDIAN__
  242. *(vhalf *) PA_83902_IF = ((half) buf[1] << 8) | buf[0];
  243. #else
  244. *(vhalf *) PA_83902_IF = ((half) buf[0] << 8) | buf[1];
  245. #endif
  246. STNIC_DELAY ();
  247. buf += sizeof (half);
  248. length -= sizeof (half);
  249. }
  250. STNIC_WRITE (STNIC_CR, CR_RDMA | CR_PG0 | CR_STA);
  251. }
  252. /* This function resets the STNIC if something screws up. */
  253. static void
  254. stnic_init (struct net_device *dev)
  255. {
  256. stnic_reset (dev);
  257. NS8390_init (dev, 0);
  258. return;
  259. }
  260. static void __exit stnic_cleanup(void)
  261. {
  262. unregister_netdev(stnic_dev);
  263. free_irq(stnic_dev->irq, stnic_dev);
  264. free_netdev(stnic_dev);
  265. }
  266. module_init(stnic_probe);
  267. module_exit(stnic_cleanup);
  268. MODULE_LICENSE("GPL");