You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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