選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

3c509.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Split out into 3c509.c and 3c5x9.c, to make it possible to build a
  3. * 3c529 module without including ISA, ISAPnP and EISA code.
  4. *
  5. */
  6. FILE_LICENCE ( BSD2 );
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <ipxe/io.h>
  12. #include <unistd.h>
  13. #include <ipxe/device.h>
  14. #include <ipxe/isa.h>
  15. #include "3c509.h"
  16. /*
  17. * 3c509 cards have their own method of contention resolution; this
  18. * effectively defines another bus type similar to ISAPnP. Even the
  19. * original ISA cards can be programatically mapped to any I/O address
  20. * in the range 0x200-0x3e0.
  21. *
  22. * However, there is a small problem: once you've activated a card,
  23. * the only ways to deactivate it will also wipe its tag, meaning that
  24. * you won't be able to subsequently reactivate it without going
  25. * through the whole ID sequence again. The solution we adopt is to
  26. * isolate and tag all cards at the start, and to immediately
  27. * re-isolate and re-tag a card after disabling it.
  28. *
  29. */
  30. static void t509bus_remove ( struct root_device *rootdev );
  31. static unsigned int t509_id_port = 0;
  32. static unsigned int t509_max_tag = 0;
  33. /** A 3c509 device */
  34. struct t509_device {
  35. /** Generic device */
  36. struct device dev;
  37. /** Tag */
  38. unsigned int tag;
  39. /** I/O address */
  40. uint16_t ioaddr;
  41. /** Driver-private data
  42. *
  43. * Use t509_set_drvdata() and t509_get_drvdata() to access
  44. * this field.
  45. */
  46. void *priv;
  47. };
  48. /**
  49. * Set 3c509 driver-private data
  50. *
  51. * @v t509 3c509 device
  52. * @v priv Private data
  53. */
  54. static inline void t509_set_drvdata ( struct t509_device *t509, void *priv ) {
  55. t509->priv = priv;
  56. }
  57. /**
  58. * Get 3c509 driver-private data
  59. *
  60. * @v t509 3c509 device
  61. * @ret priv Private data
  62. */
  63. static inline void * t509_get_drvdata ( struct t509_device *t509 ) {
  64. return t509->priv;
  65. }
  66. /*
  67. * t509 utility functions
  68. *
  69. */
  70. static inline void t509_set_id_port ( void ) {
  71. outb ( 0x00, t509_id_port );
  72. }
  73. static inline void t509_wait_for_id_sequence ( void ) {
  74. outb ( 0x00, t509_id_port );
  75. }
  76. static inline void t509_global_reset ( void ) {
  77. outb ( 0xc0, t509_id_port );
  78. }
  79. static inline void t509_reset_tag ( void ) {
  80. outb ( 0xd0, t509_id_port );
  81. }
  82. static inline void t509_set_tag ( uint8_t tag ) {
  83. outb ( 0xd0 | tag, t509_id_port );
  84. }
  85. static inline void t509_select_tag ( uint8_t tag ) {
  86. outb ( 0xd8 | tag, t509_id_port );
  87. }
  88. static inline void t509_activate ( uint16_t ioaddr ) {
  89. outb ( 0xe0 | ( ioaddr >> 4 ), t509_id_port );
  90. }
  91. static inline void t509_deactivate_and_reset_tag ( uint16_t ioaddr ) {
  92. outb ( GLOBAL_RESET, ioaddr + EP_COMMAND );
  93. }
  94. static inline void t509_load_eeprom_word ( uint8_t offset ) {
  95. outb ( 0x80 | offset, t509_id_port );
  96. }
  97. /*
  98. * Find a suitable ID port
  99. *
  100. */
  101. static inline int t509_find_id_port ( void ) {
  102. for ( t509_id_port = EP_ID_PORT_START ;
  103. t509_id_port < EP_ID_PORT_END ;
  104. t509_id_port += EP_ID_PORT_INC ) {
  105. t509_set_id_port ();
  106. /* See if anything's listening */
  107. outb ( 0xff, t509_id_port );
  108. if ( inb ( t509_id_port ) & 0x01 ) {
  109. /* Found a suitable port */
  110. DBG ( "T509 using ID port at %04x\n", t509_id_port );
  111. return 0;
  112. }
  113. }
  114. /* No id port available */
  115. DBG ( "T509 found no available ID port\n" );
  116. return -ENOENT;
  117. }
  118. /*
  119. * Send ID sequence to the ID port
  120. *
  121. */
  122. static void t509_send_id_sequence ( void ) {
  123. unsigned short lrs_state, i;
  124. t509_set_id_port ();
  125. /* Reset IDS on cards */
  126. t509_wait_for_id_sequence ();
  127. lrs_state = 0xff;
  128. for ( i = 0; i < 255; i++ ) {
  129. outb ( lrs_state, t509_id_port );
  130. lrs_state <<= 1;
  131. lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
  132. }
  133. }
  134. /*
  135. * We get eeprom data from the id_port given an offset into the eeprom.
  136. * Basically; after the ID_sequence is sent to all of the cards; they enter
  137. * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
  138. * the eeprom data. We then read the port 16 times and with every read; the
  139. * cards check for contention (ie: if one card writes a 0 bit and another
  140. * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
  141. * compares the data on the bus; if there is a difference then that card goes
  142. * into ID_WAIT state again). In the meantime; one bit of data is returned in
  143. * the AX register which is conveniently returned to us by inb(). Hence; we
  144. * read 16 times getting one bit of data with each read.
  145. */
  146. static uint16_t t509_id_read_eeprom ( int offset ) {
  147. int i, data = 0;
  148. t509_load_eeprom_word ( offset );
  149. /* Do we really need this wait? Won't be noticeable anyway */
  150. udelay(10000);
  151. for ( i = 0; i < 16; i++ ) {
  152. data = ( data << 1 ) | ( inw ( t509_id_port ) & 1 );
  153. }
  154. return data;
  155. }
  156. /*
  157. * Isolate and tag all t509 cards
  158. *
  159. */
  160. static int t509_isolate ( void ) {
  161. unsigned int i;
  162. uint16_t contend[3];
  163. int rc;
  164. /* Find a suitable ID port */
  165. if ( ( rc = t509_find_id_port() ) != 0 )
  166. return rc;
  167. while ( 1 ) {
  168. /* All cards are in ID_WAIT state each time we go
  169. * through this loop.
  170. */
  171. /* Send the ID sequence */
  172. t509_send_id_sequence();
  173. /* First time through, reset all tags. On subsequent
  174. * iterations, kill off any already-tagged cards
  175. */
  176. if ( t509_max_tag == 0 ) {
  177. t509_reset_tag();
  178. } else {
  179. t509_select_tag ( 0 );
  180. }
  181. /* Read the manufacturer ID, to see if there are any
  182. * more cards
  183. */
  184. if ( t509_id_read_eeprom ( EEPROM_MFG_ID ) != MFG_ID ) {
  185. DBG ( "T509 saw %s signs of life\n",
  186. t509_max_tag ? "no further" : "no" );
  187. break;
  188. }
  189. /* Perform contention selection on the MAC address */
  190. for ( i = 0 ; i < 3 ; i++ ) {
  191. contend[i] = t509_id_read_eeprom ( i );
  192. }
  193. /* Only one device will still be left alive. Tag it. */
  194. ++t509_max_tag;
  195. DBG ( "T509 found card %04x%04x%04x, assigning tag %02x\n",
  196. contend[0], contend[1], contend[2], t509_max_tag );
  197. t509_set_tag ( t509_max_tag );
  198. /* Return all cards back to ID_WAIT state */
  199. t509_wait_for_id_sequence();
  200. }
  201. DBG ( "T509 found %d cards using ID port %04x\n",
  202. t509_max_tag, t509_id_port );
  203. return 0;
  204. }
  205. /*
  206. * Activate a T509 device
  207. *
  208. * The device will be enabled at whatever ioaddr is specified in the
  209. * struct t509_device; there is no need to stick with the default
  210. * ioaddr read from the EEPROM.
  211. *
  212. */
  213. static inline void activate_t509_device ( struct t509_device *t509 ) {
  214. t509_send_id_sequence ();
  215. t509_select_tag ( t509->tag );
  216. t509_activate ( t509->ioaddr );
  217. DBG ( "T509 activated device %02x at ioaddr %04x\n",
  218. t509->tag, t509->ioaddr );
  219. }
  220. /*
  221. * Deactivate a T509 device
  222. *
  223. * Disabling also clears the tag, so we immediately isolate and re-tag
  224. * this card.
  225. *
  226. */
  227. static inline void deactivate_t509_device ( struct t509_device *t509 ) {
  228. t509_deactivate_and_reset_tag ( t509->ioaddr );
  229. udelay ( 1000 );
  230. t509_send_id_sequence ();
  231. t509_select_tag ( 0 );
  232. t509_set_tag ( t509->tag );
  233. t509_wait_for_id_sequence ();
  234. DBG ( "T509 deactivated device at %04x and re-tagged as %02x\n",
  235. t509->ioaddr, t509->tag );
  236. }
  237. /*
  238. * The ISA probe function
  239. *
  240. */
  241. static int legacy_t509_probe ( struct nic *nic, void *hwdev ) {
  242. struct t509_device *t509 = hwdev;
  243. /* We could change t509->ioaddr if we wanted to */
  244. activate_t509_device ( t509 );
  245. nic->ioaddr = t509->ioaddr;
  246. /* Hand off to generic t5x9 probe routine */
  247. return t5x9_probe ( nic, ISA_PROD_ID ( PROD_ID ), ISA_PROD_ID_MASK );
  248. }
  249. static void legacy_t509_disable ( struct nic *nic, void *hwdev ) {
  250. struct t509_device *t509 = hwdev;
  251. t5x9_disable ( nic );
  252. deactivate_t509_device ( t509 );
  253. }
  254. static inline void legacy_t509_set_drvdata ( void *hwdev, void *priv ) {
  255. t509_set_drvdata ( hwdev, priv );
  256. }
  257. static inline void * legacy_t509_get_drvdata ( void *hwdev ) {
  258. return t509_get_drvdata ( hwdev );
  259. }
  260. /**
  261. * Probe a 3c509 device
  262. *
  263. * @v t509 3c509 device
  264. * @ret rc Return status code
  265. *
  266. * Searches for a driver for the 3c509 device. If a driver is found,
  267. * its probe() routine is called.
  268. */
  269. static int t509_probe ( struct t509_device *t509 ) {
  270. DBG ( "Adding 3c509 device %02x (I/O %04x)\n",
  271. t509->tag, t509->ioaddr );
  272. return legacy_probe ( t509, legacy_t509_set_drvdata, &t509->dev,
  273. legacy_t509_probe, legacy_t509_disable );
  274. }
  275. /**
  276. * Remove a 3c509 device
  277. *
  278. * @v t509 3c509 device
  279. */
  280. static void t509_remove ( struct t509_device *t509 ) {
  281. legacy_remove ( t509, legacy_t509_get_drvdata, legacy_t509_disable );
  282. DBG ( "Removed 3c509 device %02x\n", t509->tag );
  283. }
  284. /**
  285. * Probe 3c509 root bus
  286. *
  287. * @v rootdev 3c509 bus root device
  288. *
  289. * Scans the 3c509 bus for devices and registers all devices it can
  290. * find.
  291. */
  292. static int t509bus_probe ( struct root_device *rootdev ) {
  293. struct t509_device *t509 = NULL;
  294. unsigned int tag;
  295. unsigned int iobase;
  296. int rc;
  297. /* Perform isolation and tagging */
  298. if ( ( rc = t509_isolate() ) != 0 )
  299. return rc;
  300. for ( tag = 1 ; tag <= t509_max_tag ; tag++ ) {
  301. /* Allocate struct t509_device */
  302. if ( ! t509 )
  303. t509 = malloc ( sizeof ( *t509 ) );
  304. if ( ! t509 ) {
  305. rc = -ENOMEM;
  306. goto err;
  307. }
  308. memset ( t509, 0, sizeof ( *t509 ) );
  309. t509->tag = tag;
  310. /* Send the ID sequence */
  311. t509_send_id_sequence ();
  312. /* Select the specified tag */
  313. t509_select_tag ( t509->tag );
  314. /* Read the default I/O address */
  315. iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
  316. t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
  317. /* Send card back to ID_WAIT */
  318. t509_wait_for_id_sequence();
  319. /* Add to device hierarchy */
  320. snprintf ( t509->dev.name, sizeof ( t509->dev.name ),
  321. "t509%02x", tag );
  322. t509->dev.desc.bus_type = BUS_TYPE_ISA;
  323. t509->dev.desc.vendor = MFG_ID;
  324. t509->dev.desc.device = PROD_ID;
  325. t509->dev.parent = &rootdev->dev;
  326. list_add ( &t509->dev.siblings, &rootdev->dev.children );
  327. INIT_LIST_HEAD ( &t509->dev.children );
  328. /* Look for a driver */
  329. if ( t509_probe ( t509 ) == 0 ) {
  330. /* t509dev registered, we can drop our ref */
  331. t509 = NULL;
  332. } else {
  333. /* Not registered; re-use struct */
  334. list_del ( &t509->dev.siblings );
  335. }
  336. }
  337. free ( t509 );
  338. return 0;
  339. err:
  340. free ( t509 );
  341. t509bus_remove ( rootdev );
  342. return rc;
  343. }
  344. /**
  345. * Remove 3c509 root bus
  346. *
  347. * @v rootdev 3c509 bus root device
  348. */
  349. static void t509bus_remove ( struct root_device *rootdev ) {
  350. struct t509_device *t509;
  351. struct t509_device *tmp;
  352. list_for_each_entry_safe ( t509, tmp, &rootdev->dev.children,
  353. dev.siblings ) {
  354. t509_remove ( t509 );
  355. list_del ( &t509->dev.siblings );
  356. free ( t509 );
  357. }
  358. }
  359. /** 3c509 bus root device driver */
  360. static struct root_driver t509_root_driver = {
  361. .probe = t509bus_probe,
  362. .remove = t509bus_remove,
  363. };
  364. /** 3c509 bus root device */
  365. struct root_device t509_root_device __root_device = {
  366. .dev = { .name = "3c509" },
  367. .driver = &t509_root_driver,
  368. };
  369. ISA_ROM ( "3c509", "3c509" );