tc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* $OpenBSD: tc.c,v 1.20 2010/11/11 17:54:54 miod Exp $ */
  2. /* $NetBSD: tc.c,v 1.29 2001/11/13 06:26:10 lukem Exp $ */
  3. /*
  4. * Copyright (c) 1994, 1995 Carnegie-Mellon University.
  5. * All rights reserved.
  6. *
  7. * Author: Chris G. Demetriou
  8. *
  9. * Permission to use, copy, modify and distribute this software and
  10. * its documentation is hereby granted, provided that both the copyright
  11. * notice and this permission notice appear in all copies of the
  12. * software, derivative works or modified versions, and any portions
  13. * thereof, and that both notices appear in supporting documentation.
  14. *
  15. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  16. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
  17. * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  18. *
  19. * Carnegie Mellon requests users of this software to return to
  20. *
  21. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  22. * School of Computer Science
  23. * Carnegie Mellon University
  24. * Pittsburgh PA 15213-3890
  25. *
  26. * any improvements or extensions that they make and grant Carnegie the
  27. * rights to redistribute these changes.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <sys/device.h>
  32. #include <dev/tc/tcreg.h>
  33. #include <dev/tc/tcvar.h>
  34. /* Definition of the driver for autoconfig. */
  35. int tcmatch(struct device *, void *, void *);
  36. void tcattach(struct device *, struct device *, void *);
  37. struct cfattach tc_ca = {
  38. sizeof(struct tc_softc), tcmatch, tcattach
  39. };
  40. struct cfdriver tc_cd = {
  41. NULL, "tc", DV_DULL
  42. };
  43. int tcprint(void *, const char *);
  44. int tcsubmatch(struct device *, void *, void *);
  45. int tc_checkslot(tc_addr_t, char *);
  46. void tc_devinfo(const char *, char *, size_t);
  47. int
  48. tcmatch(parent, vcf, aux)
  49. struct device *parent;
  50. void *vcf, *aux;
  51. {
  52. struct tcbus_attach_args *tba = aux;
  53. struct cfdata *cf = vcf;
  54. if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
  55. return (0);
  56. return (1);
  57. }
  58. void
  59. tcattach(parent, self, aux)
  60. struct device *parent;
  61. struct device *self;
  62. void *aux;
  63. {
  64. struct tc_softc *sc = (struct tc_softc *)self;
  65. struct tcbus_attach_args *tba = aux;
  66. struct tc_attach_args ta;
  67. const struct tc_builtin *builtin;
  68. struct tc_slotdesc *slot;
  69. tc_addr_t tcaddr;
  70. int i;
  71. if (tba->tba_speed & 1)
  72. printf(": %d.5 MHz clock\n", tba->tba_speed / 2);
  73. else
  74. printf(": %d MHz clock\n", tba->tba_speed / 2);
  75. /*
  76. * Save important CPU/chipset information.
  77. */
  78. sc->sc_speed = tba->tba_speed;
  79. sc->sc_nslots = tba->tba_nslots;
  80. sc->sc_slots = tba->tba_slots;
  81. sc->sc_intr_establish = tba->tba_intr_establish;
  82. sc->sc_intr_disestablish = tba->tba_intr_disestablish;
  83. sc->sc_get_dma_tag = tba->tba_get_dma_tag;
  84. /*
  85. * Try to configure each built-in device
  86. */
  87. for (i = 0; i < tba->tba_nbuiltins; i++) {
  88. builtin = &tba->tba_builtins[i];
  89. /* sanity check! */
  90. if (builtin->tcb_slot > sc->sc_nslots)
  91. panic("tcattach: builtin %d slot > nslots", i);
  92. /*
  93. * Make sure device is really there, because some
  94. * built-in devices are really optional.
  95. */
  96. tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
  97. builtin->tcb_offset;
  98. if (tc_badaddr(tcaddr))
  99. continue;
  100. /*
  101. * Set up the device attachment information.
  102. */
  103. strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
  104. ta.ta_memt = tba->tba_memt;
  105. ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot);
  106. ta.ta_modname[TC_ROM_LLEN] = '\0';
  107. ta.ta_slot = builtin->tcb_slot;
  108. ta.ta_offset = builtin->tcb_offset;
  109. ta.ta_addr = tcaddr;
  110. ta.ta_cookie = builtin->tcb_cookie;
  111. ta.ta_busspeed = sc->sc_speed;
  112. /*
  113. * Mark the slot as used, so we don't check it later.
  114. */
  115. sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
  116. /*
  117. * Attach the device.
  118. */
  119. config_found_sm(self, &ta, tcprint, tcsubmatch);
  120. }
  121. /*
  122. * Try to configure each unused slot, last to first.
  123. */
  124. for (i = sc->sc_nslots - 1; i >= 0; i--) {
  125. slot = &sc->sc_slots[i];
  126. /* If already checked above, don't look again now. */
  127. if (slot->tcs_used)
  128. continue;
  129. /*
  130. * Make sure something is there, and find out what it is.
  131. */
  132. tcaddr = slot->tcs_addr;
  133. if (tc_badaddr(tcaddr))
  134. continue;
  135. if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
  136. continue;
  137. /*
  138. * Set up the rest of the attachment information.
  139. */
  140. ta.ta_memt = tba->tba_memt;
  141. ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
  142. ta.ta_slot = i;
  143. ta.ta_offset = 0;
  144. ta.ta_addr = tcaddr;
  145. ta.ta_cookie = slot->tcs_cookie;
  146. /*
  147. * Mark the slot as used.
  148. */
  149. slot->tcs_used = 1;
  150. /*
  151. * Attach the device.
  152. */
  153. config_found_sm(self, &ta, tcprint, tcsubmatch);
  154. }
  155. }
  156. int
  157. tcprint(aux, pnp)
  158. void *aux;
  159. const char *pnp;
  160. {
  161. struct tc_attach_args *ta = aux;
  162. char devinfo[256];
  163. if (pnp) {
  164. tc_devinfo(ta->ta_modname, devinfo, sizeof devinfo);
  165. printf("%s at %s", devinfo, pnp);
  166. }
  167. printf(" slot %d offset 0x%lx", ta->ta_slot,
  168. (long)ta->ta_offset);
  169. return (UNCONF);
  170. }
  171. int
  172. tcsubmatch(parent, vcf, aux)
  173. struct device *parent;
  174. void *vcf, *aux;
  175. {
  176. struct tc_attach_args *d = aux;
  177. struct cfdata *cf = vcf;
  178. if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
  179. (cf->tccf_slot != d->ta_slot))
  180. return 0;
  181. if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
  182. (cf->tccf_offset != d->ta_offset))
  183. return 0;
  184. return ((*cf->cf_attach->ca_match)(parent, cf, aux));
  185. }
  186. #define NTC_ROMOFFS 2
  187. static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
  188. TC_SLOT_ROM,
  189. TC_SLOT_PROTOROM,
  190. };
  191. int
  192. tc_checkslot(slotbase, namep)
  193. tc_addr_t slotbase;
  194. char *namep;
  195. {
  196. struct tc_rommap *romp;
  197. int i, j;
  198. for (i = 0; i < NTC_ROMOFFS; i++) {
  199. romp = (struct tc_rommap *)
  200. (slotbase + tc_slot_romoffs[i]);
  201. switch (romp->tcr_width.v) {
  202. case 1:
  203. case 2:
  204. case 4:
  205. break;
  206. default:
  207. continue;
  208. }
  209. if (romp->tcr_stride.v != 4)
  210. continue;
  211. for (j = 0; j < 4; j++)
  212. if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
  213. romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
  214. romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
  215. romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
  216. continue;
  217. for (j = 0; j < TC_ROM_LLEN; j++)
  218. namep[j] = romp->tcr_modname[j].v;
  219. namep[j] = '\0';
  220. return (1);
  221. }
  222. return (0);
  223. }
  224. void
  225. tc_intr_establish(dev, cookie, level, handler, arg, name)
  226. struct device *dev;
  227. void *cookie, *arg;
  228. int level;
  229. int (*handler)(void *);
  230. const char *name;
  231. {
  232. struct tc_softc *sc = tc_cd.cd_devs[0];
  233. (*sc->sc_intr_establish)(dev, cookie, level, handler, arg, name);
  234. }
  235. void
  236. tc_intr_disestablish(dev, cookie, name)
  237. struct device *dev;
  238. void *cookie;
  239. const char *name;
  240. {
  241. struct tc_softc *sc = tc_cd.cd_devs[0];
  242. (*sc->sc_intr_disestablish)(dev, cookie, name);
  243. }
  244. #ifdef TCVERBOSE
  245. /*
  246. * Descriptions of known devices.
  247. */
  248. struct tc_knowndev {
  249. const char *id, *description;
  250. };
  251. #include <dev/tc/tcdevs_data.h>
  252. #endif /* TCVERBOSE */
  253. void
  254. tc_devinfo(const char *id, char *cp, size_t cp_len)
  255. {
  256. #ifdef TCVERBOSE
  257. struct tc_knowndev *tdp;
  258. const char *description;
  259. /* find the device in the table, if possible. */
  260. description = NULL;
  261. for (tdp = tc_knowndevs; tdp->id != NULL; tdp++) {
  262. /* check this entry for a match */
  263. if (strcmp(tdp->id, id) == 0) {
  264. description = tdp->description;
  265. break;
  266. }
  267. }
  268. if (description != NULL)
  269. snprintf(cp, cp_len, "\"%s\" (%s)", id, description);
  270. else
  271. #endif
  272. snprintf(cp, cp_len, "\"%s\"", id);
  273. }