magma.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. /* $OpenBSD: magma.c,v 1.25 2015/02/10 21:56:09 miod Exp $ */
  2. /*-
  3. * Copyright (c) 1998 Iain Hibbert
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* #define MAGMA_DEBUG */
  27. /*
  28. * Driver for Magma SBus Serial/Parallel cards using the Cirrus Logic
  29. * CD1400 & CD1190 chips
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/proc.h>
  34. #include <sys/device.h>
  35. #include <sys/file.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/malloc.h>
  38. #include <sys/tty.h>
  39. #include <sys/time.h>
  40. #include <sys/kernel.h>
  41. #include <sys/syslog.h>
  42. #include <sys/conf.h>
  43. #include <sys/errno.h>
  44. #include <machine/autoconf.h>
  45. #include <machine/conf.h>
  46. #include <machine/bus.h>
  47. #include <machine/bppioctl.h>
  48. #include <dev/sbus/sbusvar.h>
  49. #include <dev/ic/cd1400reg.h>
  50. #include <dev/ic/cd1190reg.h>
  51. #include <dev/sbus/magmareg.h>
  52. /* supported cards
  53. *
  54. * The table below lists the cards that this driver is likely to
  55. * be able to support.
  56. *
  57. * Cards with parallel ports: except for the LC2+1Sp, they all use
  58. * the CD1190 chip which I know nothing about. I've tried to leave
  59. * hooks for it so it shouldn't be too hard to add support later.
  60. * (I think somebody is working on this separately)
  61. *
  62. * Thanks to Bruce at Magma for telling me the hardware offsets.
  63. */
  64. static const struct magma_board_info supported_cards[] = {
  65. {
  66. "MAGMA_Sp", "MAGMA,4_Sp", "Magma 4 Sp", 4, 0,
  67. 1, 0xa000, 0xc000, 0xe000, { 0x8000, 0, 0, 0 },
  68. 0, { 0, 0 }
  69. },
  70. {
  71. "MAGMA_Sp", "MAGMA,8_Sp", "Magma 8 Sp", 8, 0,
  72. 2, 0xa000, 0xc000, 0xe000, { 0x4000, 0x6000, 0, 0 },
  73. 0, { 0, 0 }
  74. },
  75. {
  76. "MAGMA_Sp", "MAGMA,_8HS_Sp", "Magma Fast 8 Sp", 8, 0,
  77. 2, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0, 0 },
  78. 0, { 0, 0 }
  79. },
  80. {
  81. "MAGMA_Sp", "MAGMA,_8SP_422", "Magma 8 Sp - 422", 8, 0,
  82. 2, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0, 0 },
  83. 0, { 0, 0 }
  84. },
  85. {
  86. "MAGMA_Sp", "MAGMA,12_Sp", "Magma 12 Sp", 12, 0,
  87. 3, 0xa000, 0xc000, 0xe000, { 0x2000, 0x4000, 0x6000, 0 },
  88. 0, { 0, 0 }
  89. },
  90. {
  91. "MAGMA_Sp", "MAGMA,16_Sp", "Magma 16 Sp", 16, 0,
  92. 4, 0xd000, 0xe000, 0xf000, { 0x8000, 0x9000, 0xa000, 0xb000 },
  93. 0, { 0, 0 }
  94. },
  95. {
  96. "MAGMA_Sp", "MAGMA,16_Sp_2", "Magma 16 Sp", 16, 0,
  97. 4, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0xc000, 0xe000 },
  98. 0, { 0, 0 }
  99. },
  100. {
  101. "MAGMA_Sp", "MAGMA,16HS_Sp", "Magma Fast 16 Sp", 16, 0,
  102. 4, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0xc000, 0xe000 },
  103. 0, { 0, 0 }
  104. },
  105. {
  106. "MAGMA_Sp", "MAGMA,21_Sp", "Magma LC 2+1 Sp", 2, 1,
  107. 1, 0xa000, 0xc000, 0xe000, { 0x8000, 0, 0, 0 },
  108. 0, { 0, 0 }
  109. },
  110. {
  111. "MAGMA_Sp", "MAGMA,21HS_Sp", "Magma 2+1 Sp", 2, 1,
  112. 1, 0xa000, 0xc000, 0xe000, { 0x4000, 0, 0, 0 },
  113. 1, { 0x6000, 0 }
  114. },
  115. {
  116. "MAGMA_Sp", "MAGMA,41_Sp", "Magma 4+1 Sp", 4, 1,
  117. 1, 0xa000, 0xc000, 0xe000, { 0x4000, 0, 0, 0 },
  118. 1, { 0x6000, 0 }
  119. },
  120. {
  121. "MAGMA_Sp", "MAGMA,82_Sp", "Magma 8+2 Sp", 8, 2,
  122. 2, 0xd000, 0xe000, 0xf000, { 0x8000, 0x9000, 0, 0 },
  123. 2, { 0xa000, 0xb000 }
  124. },
  125. {
  126. "MAGMA_Sp", "MAGMA,P1_Sp", "Magma P1 Sp", 0, 1,
  127. 0, 0, 0, 0, { 0, 0, 0, 0 },
  128. 1, { 0x8000, 0 }
  129. },
  130. {
  131. "MAGMA_Sp", "MAGMA,P2_Sp", "Magma P2 Sp", 0, 2,
  132. 0, 0, 0, 0, { 0, 0, 0, 0 },
  133. 2, { 0x4000, 0x8000 }
  134. },
  135. {
  136. "MAGMA 2+1HS Sp", "", "Magma 2+1HS Sp", 2, 0,
  137. 1, 0xa000, 0xc000, 0xe000, { 0x4000, 0, 0, 0 },
  138. 1, { 0x8000, 0 }
  139. },
  140. {
  141. NULL, NULL, NULL, 0, 0,
  142. 0, 0, 0, 0, { 0, 0, 0, 0 },
  143. 0, { 0, 0 }
  144. }
  145. };
  146. /************************************************************************
  147. *
  148. * Autoconfig Stuff
  149. */
  150. struct cfattach magma_ca = {
  151. sizeof(struct magma_softc), magma_match, magma_attach
  152. };
  153. struct cfdriver magma_cd = {
  154. NULL, "magma", DV_DULL
  155. };
  156. struct cfattach mtty_ca = {
  157. sizeof(struct mtty_softc), mtty_match, mtty_attach
  158. };
  159. struct cfdriver mtty_cd = {
  160. NULL, "mtty", DV_TTY
  161. };
  162. struct cfattach mbpp_ca = {
  163. sizeof(struct mbpp_softc), mbpp_match, mbpp_attach
  164. };
  165. struct cfdriver mbpp_cd = {
  166. NULL, "mbpp", DV_DULL
  167. };
  168. /************************************************************************
  169. *
  170. * CD1400 Routines
  171. *
  172. * cd1400_compute_baud calculate COR/BPR register values
  173. * cd1400_write_ccr write a value to CD1400 ccr
  174. * cd1400_read_reg read from a CD1400 register
  175. * cd1400_write_reg write to a CD1400 register
  176. * cd1400_enable_transmitter enable transmitting on CD1400 channel
  177. */
  178. /*
  179. * compute the bpr/cor pair for any baud rate
  180. * returns 0 for success, 1 for failure
  181. */
  182. int
  183. cd1400_compute_baud(speed_t speed, int clock, int *cor, int *bpr)
  184. {
  185. int c, co, br;
  186. if (speed < 50 || speed > 150000)
  187. return (1);
  188. for (c = 0, co = 8 ; co <= 2048 ; co <<= 2, c++) {
  189. br = ((clock * 1000000) + (co * speed) / 2) / (co * speed);
  190. if (br < 0x100) {
  191. *bpr = br;
  192. *cor = c;
  193. return (0);
  194. }
  195. }
  196. return (1);
  197. }
  198. #define CD1400_READ_REG(cd,reg) \
  199. bus_space_read_1((cd)->cd_regt, (cd)->cd_regh, (reg))
  200. #define CD1400_WRITE_REG(cd,reg,value) \
  201. bus_space_write_1((cd)->cd_regt, (cd)->cd_regh, (reg), (value))
  202. /*
  203. * Write a CD1400 channel command, should have a timeout?
  204. */
  205. __inline void
  206. cd1400_write_ccr(struct cd1400 *cd, u_char cmd)
  207. {
  208. while (CD1400_READ_REG(cd, CD1400_CCR))
  209. /*EMPTY*/;
  210. CD1400_WRITE_REG(cd, CD1400_CCR, cmd);
  211. }
  212. /*
  213. * enable transmit service requests for cd1400 channel
  214. */
  215. void
  216. cd1400_enable_transmitter(struct cd1400 *cd, int channel)
  217. {
  218. int s, srer;
  219. s = spltty();
  220. CD1400_WRITE_REG(cd, CD1400_CAR, channel);
  221. srer = CD1400_READ_REG(cd, CD1400_SRER);
  222. SET(srer, CD1400_SRER_TXRDY);
  223. CD1400_WRITE_REG(cd, CD1400_SRER, srer);
  224. splx(s);
  225. }
  226. /************************************************************************
  227. *
  228. * CD1190 Routines
  229. */
  230. /* well, there are none yet */
  231. /************************************************************************
  232. *
  233. * Magma Routines
  234. *
  235. * magma_match reports if we have a magma board available
  236. * magma_attach attaches magma boards to the sbus
  237. * magma_hard hardware level interrupt routine
  238. * magma_soft software level interrupt routine
  239. */
  240. int
  241. magma_match(struct device *parent, void *vcf, void *aux)
  242. {
  243. struct sbus_attach_args *sa = aux;
  244. const struct magma_board_info *card;
  245. /* See if we support this device */
  246. for (card = supported_cards; ; card++) {
  247. if (card->mb_sbusname == NULL)
  248. /* End of table: no match */
  249. return (0);
  250. if (strcmp(sa->sa_name, card->mb_sbusname) == 0)
  251. break;
  252. }
  253. return (1);
  254. }
  255. void
  256. magma_attach(struct device *parent, struct device *dev, void *aux)
  257. {
  258. struct sbus_attach_args *sa = aux;
  259. struct magma_softc *sc = (struct magma_softc *)dev;
  260. const struct magma_board_info *card;
  261. char magma_prom[40], *clockstr;
  262. int chip, cd_clock;
  263. getpropstringA(sa->sa_node, "magma_prom", magma_prom);
  264. for (card = supported_cards; card->mb_name != NULL; card++) {
  265. if (strcmp(sa->sa_name, card->mb_sbusname) != 0)
  266. continue;
  267. if (strcmp(magma_prom, card->mb_name) == 0)
  268. break;
  269. }
  270. if (card->mb_name == NULL) {
  271. printf(": %s (unsupported)\n", magma_prom);
  272. return;
  273. }
  274. sc->sc_bustag = sa->sa_bustag;
  275. clockstr = getpropstring(sa->sa_node, "clock");
  276. if (strlen(clockstr) == 0)
  277. cd_clock = 25;
  278. else {
  279. cd_clock = 0;
  280. while (*clockstr != '\0')
  281. cd_clock = cd_clock * 10 + *clockstr++ - '0';
  282. }
  283. if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
  284. sa->sa_reg[0].sbr_offset, sa->sa_reg[0].sbr_size,
  285. 0, 0, &sc->sc_iohandle) != 0) {
  286. printf(": can't map registers\n");
  287. return;
  288. }
  289. if (sa->sa_nintr < 1) {
  290. printf(": can't find interrupt\n");
  291. return;
  292. }
  293. sc->sc_ih = bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY, 0,
  294. magma_hard, sc, dev->dv_xname);
  295. if (sc->sc_ih == NULL) {
  296. printf(": couldn't establish interrupt, pri %d\n",
  297. INTLEV(sa->sa_pri));
  298. bus_space_unmap(sc->sc_bustag, sc->sc_iohandle,
  299. sa->sa_reg[0].sbr_size);
  300. return;
  301. }
  302. sc->sc_sih = softintr_establish(IPL_TTY, magma_soft, sc);
  303. if (sc->sc_sih == NULL) {
  304. printf(": can't get soft intr\n");
  305. bus_space_unmap(sc->sc_bustag, sc->sc_iohandle,
  306. sa->sa_reg[0].sbr_size);
  307. return;
  308. }
  309. printf(": %s\n", card->mb_realname);
  310. sc->ms_board = card;
  311. sc->ms_ncd1400 = card->mb_ncd1400;
  312. sc->ms_ncd1190 = card->mb_ncd1190;
  313. /* the SVCACK* lines are daisychained */
  314. if (bus_space_subregion(sc->sc_bustag, sc->sc_iohandle,
  315. card->mb_svcackr, 1, &sc->sc_svcackrh)) {
  316. printf(": failed to map svcackr\n");
  317. return;
  318. }
  319. if (bus_space_subregion(sc->sc_bustag, sc->sc_iohandle,
  320. card->mb_svcackt, 1, &sc->sc_svcackth)) {
  321. printf(": failed to map svcackt\n");
  322. return;
  323. }
  324. if (bus_space_subregion(sc->sc_bustag, sc->sc_iohandle,
  325. card->mb_svcackm, 1, &sc->sc_svcackmh)) {
  326. printf(": failed to map svcackm\n");
  327. return;
  328. }
  329. /* init the cd1400 chips */
  330. for (chip = 0 ; chip < card->mb_ncd1400 ; chip++) {
  331. struct cd1400 *cd = &sc->ms_cd1400[chip];
  332. cd->cd_clock = cd_clock;
  333. if (bus_space_subregion(sc->sc_bustag, sc->sc_iohandle,
  334. card->mb_cd1400[chip], CD1400_REGMAPSIZE, &cd->cd_regh)) {
  335. printf(": failed to map cd1400 regs\n");
  336. return;
  337. }
  338. cd->cd_regt = sc->sc_bustag;
  339. /* getpropstring(sa->sa_node, "chiprev"); */
  340. /* seemingly the Magma drivers just ignore the propstring */
  341. cd->cd_chiprev = CD1400_READ_REG(cd, CD1400_GFRCR);
  342. dprintf(("%s attach CD1400 %d addr 0x%x rev %x clock %dMHz\n",
  343. sc->ms_dev.dv_xname, chip, cd->cd_reg,
  344. cd->cd_chiprev, cd->cd_clock));
  345. /* clear GFRCR */
  346. CD1400_WRITE_REG(cd, CD1400_GFRCR, 0x00);
  347. /* reset whole chip */
  348. cd1400_write_ccr(cd,
  349. CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
  350. /* wait for revision code to be restored */
  351. while (CD1400_READ_REG(cd, CD1400_GFRCR) != cd->cd_chiprev)
  352. ;
  353. /* set the Prescaler Period Register to tick at 1ms */
  354. CD1400_WRITE_REG(cd, CD1400_PPR,
  355. ((cd->cd_clock * 1000000 / CD1400_PPR_PRESCALER + 500)
  356. / 1000));
  357. /*
  358. * The LC2+1Sp card is the only card that doesn't have a
  359. * CD1190 for the parallel port, but uses channel 0 of the
  360. * CD1400, so we make a note of it for later and set up the
  361. * CD1400 for parallel mode operation.
  362. */
  363. if (card->mb_npar && card->mb_ncd1190 == 0) {
  364. CD1400_WRITE_REG(cd, CD1400_GCR, CD1400_GCR_PARALLEL);
  365. cd->cd_parmode = 1;
  366. }
  367. }
  368. /* init the cd1190 chips */
  369. for (chip = 0 ; chip < card->mb_ncd1190 ; chip++) {
  370. struct cd1190 *cd = &sc->ms_cd1190[chip];
  371. if (bus_space_subregion(sc->sc_bustag, sc->sc_iohandle,
  372. card->mb_cd1190[chip], CD1190_REGMAPSIZE, &cd->cd_regh)) {
  373. printf(": failed to map cd1190 regs\n");
  374. return;
  375. }
  376. cd->cd_regt = sc->sc_bustag;
  377. dprintf(("%s attach CD1190 %d addr 0x%x (failed)\n",
  378. sc->ms_dev.dv_xname, chip, cd->cd_reg));
  379. /* XXX don't know anything about these chips yet */
  380. }
  381. /* configure the children */
  382. (void)config_found(dev, mtty_match, NULL);
  383. (void)config_found(dev, mbpp_match, NULL);
  384. }
  385. /*
  386. * hard interrupt routine
  387. *
  388. * returns 1 if it handled it, otherwise 0
  389. *
  390. * runs at interrupt priority
  391. */
  392. int
  393. magma_hard(void *arg)
  394. {
  395. struct magma_softc *sc = arg;
  396. struct cd1400 *cd;
  397. int chip, status = 0;
  398. int serviced = 0;
  399. int needsoftint = 0;
  400. /*
  401. * check status of all the CD1400 chips
  402. */
  403. for (chip = 0 ; chip < sc->ms_ncd1400 ; chip++)
  404. status |= CD1400_READ_REG(&sc->ms_cd1400[chip], CD1400_SVRR);
  405. if (ISSET(status, CD1400_SVRR_RXRDY)) {
  406. /* enter rx service context */
  407. u_int8_t rivr = bus_space_read_1(sc->sc_bustag, sc->sc_svcackrh, 0);
  408. int port = rivr >> 4;
  409. if (rivr & (1<<3)) { /* parallel port */
  410. struct mbpp_port *mbpp;
  411. int n_chars;
  412. mbpp = &sc->ms_mbpp->ms_port[port];
  413. cd = mbpp->mp_cd1400;
  414. /* don't think we have to handle exceptions */
  415. n_chars = CD1400_READ_REG(cd, CD1400_RDCR);
  416. while (n_chars--) {
  417. if (mbpp->mp_cnt == 0) {
  418. SET(mbpp->mp_flags, MBPPF_WAKEUP);
  419. needsoftint = 1;
  420. break;
  421. }
  422. *mbpp->mp_ptr = CD1400_READ_REG(cd, CD1400_RDSR);
  423. mbpp->mp_ptr++;
  424. mbpp->mp_cnt--;
  425. }
  426. } else { /* serial port */
  427. struct mtty_port *mtty;
  428. u_char *ptr, n_chars, line_stat;
  429. mtty = &sc->ms_mtty->ms_port[port];
  430. cd = mtty->mp_cd1400;
  431. if (ISSET(rivr, CD1400_RIVR_EXCEPTION)) {
  432. line_stat = CD1400_READ_REG(cd, CD1400_RDSR);
  433. n_chars = 1;
  434. } else { /* no exception, received data OK */
  435. line_stat = 0;
  436. n_chars = CD1400_READ_REG(cd, CD1400_RDCR);
  437. }
  438. ptr = mtty->mp_rput;
  439. while (n_chars--) {
  440. *ptr++ = line_stat;
  441. *ptr++ = CD1400_READ_REG(cd, CD1400_RDSR);
  442. if (ptr == mtty->mp_rend)
  443. ptr = mtty->mp_rbuf;
  444. if (ptr == mtty->mp_rget) {
  445. if (ptr == mtty->mp_rbuf)
  446. ptr = mtty->mp_rend;
  447. ptr -= 2;
  448. SET(mtty->mp_flags,
  449. MTTYF_RING_OVERFLOW);
  450. break;
  451. }
  452. }
  453. mtty->mp_rput = ptr;
  454. needsoftint = 1;
  455. }
  456. CD1400_WRITE_REG(cd, CD1400_EOSRR, 0); /* end service context */
  457. serviced = 1;
  458. } /* if(rx_service...) */
  459. if (ISSET(status, CD1400_SVRR_MDMCH)) {
  460. u_int8_t mivr = bus_space_read_1(sc->sc_bustag, sc->sc_svcackmh, 0);
  461. int port = mivr >> 4;
  462. struct mtty_port *mtty;
  463. int carrier;
  464. u_char msvr;
  465. /*
  466. * Handle CD (LC2+1Sp = DSR) changes.
  467. */
  468. mtty = &sc->ms_mtty->ms_port[port];
  469. cd = mtty->mp_cd1400;
  470. msvr = CD1400_READ_REG(cd, CD1400_MSVR2);
  471. carrier = ISSET(msvr, cd->cd_parmode ? CD1400_MSVR2_DSR : CD1400_MSVR2_CD);
  472. if (mtty->mp_carrier != carrier) {
  473. SET(mtty->mp_flags, MTTYF_CARRIER_CHANGED);
  474. mtty->mp_carrier = carrier;
  475. needsoftint = 1;
  476. }
  477. CD1400_WRITE_REG(cd, CD1400_EOSRR, 0); /* end service context */
  478. serviced = 1;
  479. } /* if(mdm_service...) */
  480. if (ISSET(status, CD1400_SVRR_TXRDY)) {
  481. /* enter tx service context */
  482. u_int8_t tivr = bus_space_read_1(sc->sc_bustag, sc->sc_svcackth, 0);
  483. int port = tivr >> 4;
  484. if (tivr & (1<<3)) { /* parallel port */
  485. struct mbpp_port *mbpp;
  486. mbpp = &sc->ms_mbpp->ms_port[port];
  487. cd = mbpp->mp_cd1400;
  488. if (mbpp->mp_cnt) {
  489. int count = 0;
  490. /* fill the fifo */
  491. while (mbpp->mp_cnt && count++ < CD1400_PAR_FIFO_SIZE) {
  492. CD1400_WRITE_REG(cd, CD1400_TDR, *mbpp->mp_ptr);
  493. mbpp->mp_ptr++;
  494. mbpp->mp_cnt--;
  495. }
  496. } else {
  497. /* fifo is empty and we got no more data to send, so shut
  498. * off interrupts and signal for a wakeup, which can't be
  499. * done here in case we beat mbpp_send to the tsleep call
  500. * (we are running at >spltty)
  501. */
  502. CD1400_WRITE_REG(cd, CD1400_SRER, 0);
  503. SET(mbpp->mp_flags, MBPPF_WAKEUP);
  504. needsoftint = 1;
  505. }
  506. } else { /* serial port */
  507. struct mtty_port *mtty;
  508. struct tty *tp;
  509. mtty = &sc->ms_mtty->ms_port[port];
  510. cd = mtty->mp_cd1400;
  511. tp = mtty->mp_tty;
  512. if (!ISSET(mtty->mp_flags, MTTYF_STOP)) {
  513. int count = 0;
  514. /* check if we should start/stop a break */
  515. if (ISSET(mtty->mp_flags, MTTYF_SET_BREAK)) {
  516. CD1400_WRITE_REG(cd, CD1400_TDR, 0);
  517. CD1400_WRITE_REG(cd, CD1400_TDR, 0x81);
  518. /* should we delay too? */
  519. CLR(mtty->mp_flags, MTTYF_SET_BREAK);
  520. count += 2;
  521. }
  522. if (ISSET(mtty->mp_flags, MTTYF_CLR_BREAK)) {
  523. CD1400_WRITE_REG(cd, CD1400_TDR, 0);
  524. CD1400_WRITE_REG(cd, CD1400_TDR, 0x83);
  525. CLR(mtty->mp_flags, MTTYF_CLR_BREAK);
  526. count += 2;
  527. }
  528. /* I don't quite fill the fifo in case the last one is a
  529. * NULL which I have to double up because its the escape
  530. * code for embedded transmit characters.
  531. */
  532. while (mtty->mp_txc > 0 && count < CD1400_TX_FIFO_SIZE - 1) {
  533. u_char ch;
  534. ch = *mtty->mp_txp;
  535. mtty->mp_txc--;
  536. mtty->mp_txp++;
  537. if (ch == 0) {
  538. CD1400_WRITE_REG(cd, CD1400_TDR, ch);
  539. count++;
  540. }
  541. CD1400_WRITE_REG(cd, CD1400_TDR, ch);
  542. count++;
  543. }
  544. }
  545. /* if we ran out of work or are requested to STOP then
  546. * shut off the txrdy interrupts and signal DONE to flush
  547. * out the chars we have sent.
  548. */
  549. if (mtty->mp_txc == 0 || ISSET(mtty->mp_flags, MTTYF_STOP)) {
  550. int srer;
  551. srer = CD1400_READ_REG(cd, CD1400_SRER);
  552. CLR(srer, CD1400_SRER_TXRDY);
  553. CD1400_WRITE_REG(cd, CD1400_SRER, srer);
  554. CLR(mtty->mp_flags, MTTYF_STOP);
  555. SET(mtty->mp_flags, MTTYF_DONE);
  556. needsoftint = 1;
  557. }
  558. }
  559. CD1400_WRITE_REG(cd, CD1400_EOSRR, 0); /* end service context */
  560. serviced = 1;
  561. } /* if(tx_service...) */
  562. /* XXX service CD1190 interrupts too
  563. for (chip = 0 ; chip < sc->ms_ncd1190 ; chip++) {
  564. }
  565. */
  566. if (needsoftint)
  567. softintr_schedule(sc->sc_sih);
  568. return (serviced);
  569. }
  570. /*
  571. * magma soft interrupt handler
  572. *
  573. * returns 1 if it handled it, 0 otherwise
  574. *
  575. * runs at spltty()
  576. */
  577. void
  578. magma_soft(void *arg)
  579. {
  580. struct magma_softc *sc = arg;
  581. struct mtty_softc *mtty = sc->ms_mtty;
  582. struct mbpp_softc *mbpp = sc->ms_mbpp;
  583. int port;
  584. int serviced = 0;
  585. int s, flags;
  586. /*
  587. * check the tty ports (if any) to see what needs doing
  588. */
  589. if (mtty) {
  590. for (port = 0 ; port < mtty->ms_nports ; port++) {
  591. struct mtty_port *mp = &mtty->ms_port[port];
  592. struct tty *tp = mp->mp_tty;
  593. if (!ISSET(tp->t_state, TS_ISOPEN))
  594. continue;
  595. /*
  596. * handle any received data
  597. */
  598. while (mp->mp_rget != mp->mp_rput) {
  599. u_char stat;
  600. int data;
  601. stat = mp->mp_rget[0];
  602. data = mp->mp_rget[1];
  603. mp->mp_rget = ((mp->mp_rget + 2) == mp->mp_rend) ? mp->mp_rbuf : (mp->mp_rget + 2);
  604. if (stat & (CD1400_RDSR_BREAK | CD1400_RDSR_FE))
  605. data |= TTY_FE;
  606. if (stat & CD1400_RDSR_PE)
  607. data |= TTY_PE;
  608. if (stat & CD1400_RDSR_OE)
  609. log(LOG_WARNING, "%s%x: fifo overflow\n", mtty->ms_dev.dv_xname, port);
  610. (*linesw[tp->t_line].l_rint)(data, tp);
  611. serviced = 1;
  612. }
  613. s = splhigh(); /* block out hard interrupt routine */
  614. flags = mp->mp_flags;
  615. CLR(mp->mp_flags, MTTYF_DONE | MTTYF_CARRIER_CHANGED | MTTYF_RING_OVERFLOW);
  616. splx(s); /* ok */
  617. if (ISSET(flags, MTTYF_CARRIER_CHANGED)) {
  618. dprintf(("%s%x: cd %s\n", mtty->ms_dev.dv_xname, port, mp->mp_carrier ? "on" : "off"));
  619. (*linesw[tp->t_line].l_modem)(tp, mp->mp_carrier);
  620. serviced = 1;
  621. }
  622. if (ISSET(flags, MTTYF_RING_OVERFLOW)) {
  623. log(LOG_WARNING, "%s%x: ring buffer overflow\n", mtty->ms_dev.dv_xname, port);
  624. serviced = 1;
  625. }
  626. if (ISSET(flags, MTTYF_DONE)) {
  627. ndflush(&tp->t_outq, mp->mp_txp - tp->t_outq.c_cf);
  628. CLR(tp->t_state, TS_BUSY);
  629. (*linesw[tp->t_line].l_start)(tp); /* might be some more */
  630. serviced = 1;
  631. }
  632. } /* for (each mtty...) */
  633. }
  634. /*
  635. * check the bpp ports (if any) to see what needs doing
  636. */
  637. if (mbpp) {
  638. for (port = 0 ; port < mbpp->ms_nports ; port++) {
  639. struct mbpp_port *mp = &mbpp->ms_port[port];
  640. if (!ISSET(mp->mp_flags, MBPPF_OPEN))
  641. continue;
  642. s = splhigh(); /* block out hard intr routine */
  643. flags = mp->mp_flags;
  644. CLR(mp->mp_flags, MBPPF_WAKEUP);
  645. splx(s);
  646. if (ISSET(flags, MBPPF_WAKEUP)) {
  647. wakeup(mp);
  648. serviced = 1;
  649. }
  650. } /* for (each mbpp...) */
  651. }
  652. }
  653. /************************************************************************
  654. *
  655. * MTTY Routines
  656. *
  657. * mtty_match match one mtty device
  658. * mtty_attach attach mtty devices
  659. * mttyopen open mtty device
  660. * mttyclose close mtty device
  661. * mttyread read from mtty
  662. * mttywrite write to mtty
  663. * mttyioctl do ioctl on mtty
  664. * mttytty return tty pointer for mtty
  665. * mttystop stop mtty device
  666. * mtty_start start mtty device
  667. * mtty_param set mtty parameters
  668. * mtty_modem_control set modem control lines
  669. */
  670. int
  671. mtty_match(struct device *parent, void *vcf, void *args)
  672. {
  673. struct magma_softc *sc = (struct magma_softc *)parent;
  674. return (args == mtty_match && sc->ms_board->mb_nser &&
  675. sc->ms_mtty == NULL);
  676. }
  677. void
  678. mtty_attach(struct device *parent, struct device *dev, void *args)
  679. {
  680. struct magma_softc *sc = (struct magma_softc *)parent;
  681. struct mtty_softc *ms = (struct mtty_softc *)dev;
  682. int port, chip, chan;
  683. sc->ms_mtty = ms;
  684. dprintf((" addr 0x%x", ms));
  685. for (port = 0, chip = 0, chan = 0;
  686. port < sc->ms_board->mb_nser; port++) {
  687. struct mtty_port *mp = &ms->ms_port[port];
  688. struct tty *tp;
  689. mp->mp_cd1400 = &sc->ms_cd1400[chip];
  690. if (mp->mp_cd1400->cd_parmode && chan == 0) {
  691. /* skip channel 0 if parmode */
  692. chan = 1;
  693. }
  694. mp->mp_channel = chan;
  695. tp = ttymalloc(0);
  696. tp->t_oproc = mtty_start;
  697. tp->t_param = mtty_param;
  698. mp->mp_tty = tp;
  699. mp->mp_rbuf = malloc(MTTY_RBUF_SIZE, M_DEVBUF, M_NOWAIT);
  700. if (mp->mp_rbuf == NULL)
  701. break;
  702. mp->mp_rend = mp->mp_rbuf + MTTY_RBUF_SIZE;
  703. chan = (chan + 1) % CD1400_NO_OF_CHANNELS;
  704. if (chan == 0)
  705. chip++;
  706. }
  707. ms->ms_nports = port;
  708. printf(": %d tty%s\n", port, port == 1 ? "" : "s");
  709. }
  710. /*
  711. * open routine. returns zero if successful, else error code
  712. */
  713. int
  714. mttyopen(dev_t dev, int flags, int mode, struct proc *p)
  715. {
  716. int card = MAGMA_CARD(dev);
  717. int port = MAGMA_PORT(dev);
  718. struct mtty_softc *ms;
  719. struct mtty_port *mp;
  720. struct tty *tp;
  721. struct cd1400 *cd;
  722. int s;
  723. if (card >= mtty_cd.cd_ndevs || (ms = mtty_cd.cd_devs[card]) == NULL
  724. || port >= ms->ms_nports)
  725. return (ENXIO); /* device not configured */
  726. mp = &ms->ms_port[port];
  727. tp = mp->mp_tty;
  728. tp->t_dev = dev;
  729. if (!ISSET(tp->t_state, TS_ISOPEN)) {
  730. SET(tp->t_state, TS_WOPEN);
  731. /* set defaults */
  732. ttychars(tp);
  733. tp->t_iflag = TTYDEF_IFLAG;
  734. tp->t_oflag = TTYDEF_OFLAG;
  735. tp->t_cflag = TTYDEF_CFLAG;
  736. if (ISSET(mp->mp_openflags, TIOCFLAG_CLOCAL))
  737. SET(tp->t_cflag, CLOCAL);
  738. if (ISSET(mp->mp_openflags, TIOCFLAG_CRTSCTS))
  739. SET(tp->t_cflag, CRTSCTS);
  740. if (ISSET(mp->mp_openflags, TIOCFLAG_MDMBUF))
  741. SET(tp->t_cflag, MDMBUF);
  742. tp->t_lflag = TTYDEF_LFLAG;
  743. tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
  744. /* init ring buffer */
  745. mp->mp_rput = mp->mp_rget = mp->mp_rbuf;
  746. s = spltty();
  747. /* reset CD1400 channel */
  748. cd = mp->mp_cd1400;
  749. CD1400_WRITE_REG(cd, CD1400_CAR, mp->mp_channel);
  750. cd1400_write_ccr(cd, CD1400_CCR_CMDRESET);
  751. /* encode the port number in top half of LIVR */
  752. CD1400_WRITE_REG(cd, CD1400_LIVR, port << 4);
  753. /* sets parameters and raises DTR */
  754. (void)mtty_param(tp, &tp->t_termios);
  755. /* set tty watermarks */
  756. ttsetwater(tp);
  757. /* enable service requests */
  758. CD1400_WRITE_REG(cd, CD1400_SRER, CD1400_SRER_RXDATA | CD1400_SRER_MDMCH);
  759. /* tell the tty about the carrier status */
  760. if (ISSET(mp->mp_openflags, TIOCFLAG_SOFTCAR) || mp->mp_carrier)
  761. SET(tp->t_state, TS_CARR_ON);
  762. else
  763. CLR(tp->t_state, TS_CARR_ON);
  764. } else if (ISSET(tp->t_state, TS_XCLUDE) && suser(p, 0) != 0) {
  765. return (EBUSY); /* superuser can break exclusive access */
  766. } else {
  767. s = spltty();
  768. }
  769. /* wait for carrier if necessary */
  770. if (!ISSET(flags, O_NONBLOCK)) {
  771. while (!ISSET(tp->t_cflag, CLOCAL) && !ISSET(tp->t_state, TS_CARR_ON)) {
  772. int error;
  773. SET(tp->t_state, TS_WOPEN);
  774. error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, "mttydcd", 0);
  775. if (error != 0) {
  776. splx(s);
  777. CLR(tp->t_state, TS_WOPEN);
  778. return (error);
  779. }
  780. }
  781. }
  782. splx(s);
  783. return ((*linesw[tp->t_line].l_open)(dev, tp, p));
  784. }
  785. /*
  786. * close routine. returns zero if successful, else error code
  787. */
  788. int
  789. mttyclose(dev_t dev, int flag, int mode, struct proc *p)
  790. {
  791. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(dev)];
  792. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  793. struct tty *tp = mp->mp_tty;
  794. int s;
  795. (*linesw[tp->t_line].l_close)(tp, flag, p);
  796. s = spltty();
  797. /* if HUPCL is set, and the tty is no longer open
  798. * shut down the port
  799. */
  800. if (ISSET(tp->t_cflag, HUPCL) || !ISSET(tp->t_state, TS_ISOPEN)) {
  801. /* XXX wait until FIFO is empty before turning off the channel
  802. struct cd1400 *cd = mp->mp_cd1400;
  803. */
  804. /* drop DTR and RTS */
  805. (void)mtty_modem_control(mp, 0, DMSET);
  806. /* turn off the channel
  807. CD1400_WRITE_REG(cd, CD1400_CAR, mp->mp_channel);
  808. cd1400_write_ccr(cd, CD1400_CCR_CMDRESET);
  809. */
  810. }
  811. splx(s);
  812. ttyclose(tp);
  813. return (0);
  814. }
  815. /*
  816. * Read routine
  817. */
  818. int
  819. mttyread(dev_t dev, struct uio *uio, int flags)
  820. {
  821. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(dev)];
  822. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  823. struct tty *tp = mp->mp_tty;
  824. return ((*linesw[tp->t_line].l_read)(tp, uio, flags));
  825. }
  826. /*
  827. * Write routine
  828. */
  829. int
  830. mttywrite(dev_t dev, struct uio *uio, int flags)
  831. {
  832. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(dev)];
  833. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  834. struct tty *tp = mp->mp_tty;
  835. return ((*linesw[tp->t_line].l_write)(tp, uio, flags));
  836. }
  837. /*
  838. * return tty pointer
  839. */
  840. struct tty *
  841. mttytty(dev_t dev)
  842. {
  843. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(dev)];
  844. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  845. return (mp->mp_tty);
  846. }
  847. /*
  848. * ioctl routine
  849. */
  850. int
  851. mttyioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
  852. {
  853. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(dev)];
  854. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  855. struct tty *tp = mp->mp_tty;
  856. int error;
  857. error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flags, p);
  858. if (error >= 0)
  859. return (error);
  860. error = ttioctl(tp, cmd, data, flags, p);
  861. if (error >= 0)
  862. return (error);
  863. error = 0;
  864. switch(cmd) {
  865. case TIOCSBRK: /* set break */
  866. SET(mp->mp_flags, MTTYF_SET_BREAK);
  867. cd1400_enable_transmitter(mp->mp_cd1400, mp->mp_channel);
  868. break;
  869. case TIOCCBRK: /* clear break */
  870. SET(mp->mp_flags, MTTYF_CLR_BREAK);
  871. cd1400_enable_transmitter(mp->mp_cd1400, mp->mp_channel);
  872. break;
  873. case TIOCSDTR: /* set DTR */
  874. mtty_modem_control(mp, TIOCM_DTR, DMBIS);
  875. break;
  876. case TIOCCDTR: /* clear DTR */
  877. mtty_modem_control(mp, TIOCM_DTR, DMBIC);
  878. break;
  879. case TIOCMSET: /* set modem lines */
  880. mtty_modem_control(mp, *((int *)data), DMSET);
  881. break;
  882. case TIOCMBIS: /* bit set modem lines */
  883. mtty_modem_control(mp, *((int *)data), DMBIS);
  884. break;
  885. case TIOCMBIC: /* bit clear modem lines */
  886. mtty_modem_control(mp, *((int *)data), DMBIC);
  887. break;
  888. case TIOCMGET: /* get modem lines */
  889. *((int *)data) = mtty_modem_control(mp, 0, DMGET);
  890. break;
  891. case TIOCGFLAGS:
  892. *((int *)data) = mp->mp_openflags;
  893. break;
  894. case TIOCSFLAGS:
  895. if (suser(p, 0))
  896. error = EPERM;
  897. else
  898. mp->mp_openflags = *((int *)data) &
  899. (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
  900. TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
  901. break;
  902. default:
  903. error = ENOTTY;
  904. }
  905. return (error);
  906. }
  907. /*
  908. * Stop output, e.g., for ^S or output flush.
  909. */
  910. int
  911. mttystop(struct tty *tp, int flags)
  912. {
  913. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(tp->t_dev)];
  914. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(tp->t_dev)];
  915. int s;
  916. s = spltty();
  917. if (ISSET(tp->t_state, TS_BUSY)) {
  918. if (!ISSET(tp->t_state, TS_TTSTOP))
  919. SET(tp->t_state, TS_FLUSH);
  920. /*
  921. * the transmit interrupt routine will disable transmit when it
  922. * notices that MTTYF_STOP has been set.
  923. */
  924. SET(mp->mp_flags, MTTYF_STOP);
  925. }
  926. splx(s);
  927. return (0);
  928. }
  929. /*
  930. * Start output, after a stop.
  931. */
  932. void
  933. mtty_start(struct tty *tp)
  934. {
  935. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(tp->t_dev)];
  936. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(tp->t_dev)];
  937. int s;
  938. s = spltty();
  939. /* we only need to do something if we are not already busy
  940. * or delaying or stopped
  941. */
  942. if (!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
  943. /* if we are sleeping and output has drained below
  944. * low water mark, awaken
  945. */
  946. ttwakeupwr(tp);
  947. /* if something to send, start transmitting
  948. */
  949. if (tp->t_outq.c_cc) {
  950. mp->mp_txc = ndqb(&tp->t_outq, 0);
  951. mp->mp_txp = tp->t_outq.c_cf;
  952. SET(tp->t_state, TS_BUSY);
  953. cd1400_enable_transmitter(mp->mp_cd1400, mp->mp_channel);
  954. }
  955. }
  956. splx(s);
  957. }
  958. /*
  959. * set/get modem line status
  960. *
  961. * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
  962. *
  963. * note that DTR and RTS lines are exchanged, and that DSR is
  964. * not available on the LC2+1Sp card (used as CD)
  965. *
  966. * only let them fiddle with RTS if CRTSCTS is not enabled
  967. */
  968. int
  969. mtty_modem_control(struct mtty_port *mp, int bits, int howto)
  970. {
  971. struct cd1400 *cd = mp->mp_cd1400;
  972. struct tty *tp = mp->mp_tty;
  973. int s, msvr;
  974. s = spltty();
  975. CD1400_WRITE_REG(cd, CD1400_CAR, mp->mp_channel);
  976. switch(howto) {
  977. case DMGET: /* get bits */
  978. bits = 0;
  979. bits |= TIOCM_LE;
  980. msvr = CD1400_READ_REG(cd, CD1400_MSVR1);
  981. if (msvr & CD1400_MSVR1_RTS)
  982. bits |= TIOCM_DTR;
  983. msvr = CD1400_READ_REG(cd, CD1400_MSVR2);
  984. if (msvr & CD1400_MSVR2_DTR)
  985. bits |= TIOCM_RTS;
  986. if (msvr & CD1400_MSVR2_CTS)
  987. bits |= TIOCM_CTS;
  988. if (msvr & CD1400_MSVR2_RI)
  989. bits |= TIOCM_RI;
  990. if (msvr & CD1400_MSVR2_DSR)
  991. bits |= (cd->cd_parmode ? TIOCM_CD : TIOCM_DSR);
  992. if (msvr & CD1400_MSVR2_CD)
  993. bits |= (cd->cd_parmode ? 0 : TIOCM_CD);
  994. break;
  995. case DMSET: /* reset bits */
  996. if (!ISSET(tp->t_cflag, CRTSCTS))
  997. CD1400_WRITE_REG(cd, CD1400_MSVR2,
  998. ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
  999. CD1400_WRITE_REG(cd, CD1400_MSVR1,
  1000. ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
  1001. break;
  1002. case DMBIS: /* set bits */
  1003. if ((bits & TIOCM_RTS) && !ISSET(tp->t_cflag, CRTSCTS))
  1004. CD1400_WRITE_REG(cd, CD1400_MSVR2, CD1400_MSVR2_DTR);
  1005. if (bits & TIOCM_DTR)
  1006. CD1400_WRITE_REG(cd, CD1400_MSVR1, CD1400_MSVR1_RTS);
  1007. break;
  1008. case DMBIC: /* clear bits */
  1009. if ((bits & TIOCM_RTS) && !ISSET(tp->t_cflag, CRTSCTS))
  1010. CD1400_WRITE_REG(cd, CD1400_MSVR2, 0);
  1011. if (bits & TIOCM_DTR)
  1012. CD1400_WRITE_REG(cd, CD1400_MSVR1, 0);
  1013. break;
  1014. }
  1015. splx(s);
  1016. return (bits);
  1017. }
  1018. /*
  1019. * Set tty parameters, returns error or 0 on success
  1020. */
  1021. int
  1022. mtty_param(struct tty *tp, struct termios *t)
  1023. {
  1024. struct mtty_softc *ms = mtty_cd.cd_devs[MAGMA_CARD(tp->t_dev)];
  1025. struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(tp->t_dev)];
  1026. struct cd1400 *cd = mp->mp_cd1400;
  1027. int rbpr, tbpr, rcor, tcor;
  1028. u_char mcor1 = 0, mcor2 = 0;
  1029. int s, opt;
  1030. if (t->c_ospeed &&
  1031. cd1400_compute_baud(t->c_ospeed, cd->cd_clock, &tcor, &tbpr))
  1032. return (EINVAL);
  1033. if (t->c_ispeed &&
  1034. cd1400_compute_baud(t->c_ispeed, cd->cd_clock, &rcor, &rbpr))
  1035. return (EINVAL);
  1036. s = spltty();
  1037. /* hang up the line if ospeed is zero, else raise DTR */
  1038. (void)mtty_modem_control(mp, TIOCM_DTR,
  1039. (t->c_ospeed == 0 ? DMBIC : DMBIS));
  1040. /* select channel, done in mtty_modem_control() */
  1041. /* CD1400_WRITE_REG(cd, CD1400_CAR, mp->mp_channel); */
  1042. /* set transmit speed */
  1043. if (t->c_ospeed) {
  1044. CD1400_WRITE_REG(cd, CD1400_TCOR, tcor);
  1045. CD1400_WRITE_REG(cd, CD1400_TBPR, tbpr);
  1046. }
  1047. /* set receive speed */
  1048. if (t->c_ispeed) {
  1049. CD1400_WRITE_REG(cd, CD1400_RCOR, rcor);
  1050. CD1400_WRITE_REG(cd, CD1400_RBPR, rbpr);
  1051. }
  1052. /* enable transmitting and receiving on this channel */
  1053. opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN | CD1400_CCR_RCVEN;
  1054. cd1400_write_ccr(cd, opt);
  1055. /* set parity, data and stop bits */
  1056. opt = 0;
  1057. if (ISSET(t->c_cflag, PARENB))
  1058. opt |= (ISSET(t->c_cflag, PARODD) ? CD1400_COR1_PARODD : CD1400_COR1_PARNORMAL);
  1059. if (!ISSET(t->c_iflag, INPCK))
  1060. opt |= CD1400_COR1_NOINPCK; /* no parity checking */
  1061. if (ISSET(t->c_cflag, CSTOPB))
  1062. opt |= CD1400_COR1_STOP2;
  1063. switch( t->c_cflag & CSIZE) {
  1064. case CS5:
  1065. opt |= CD1400_COR1_CS5;
  1066. break;
  1067. case CS6:
  1068. opt |= CD1400_COR1_CS6;
  1069. break;
  1070. case CS7:
  1071. opt |= CD1400_COR1_CS7;
  1072. break;
  1073. default:
  1074. opt |= CD1400_COR1_CS8;
  1075. break;
  1076. }
  1077. CD1400_WRITE_REG(cd, CD1400_COR1, opt);
  1078. /*
  1079. * enable Embedded Transmit Commands (for breaks)
  1080. * use the CD1400 automatic CTS flow control if CRTSCTS is set
  1081. */
  1082. opt = CD1400_COR2_ETC;
  1083. if (ISSET(t->c_cflag, CRTSCTS))
  1084. opt |= CD1400_COR2_CCTS_OFLOW;
  1085. CD1400_WRITE_REG(cd, CD1400_COR2, opt);
  1086. CD1400_WRITE_REG(cd, CD1400_COR3, MTTY_RX_FIFO_THRESHOLD);
  1087. cd1400_write_ccr(cd, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
  1088. CD1400_WRITE_REG(cd, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
  1089. CD1400_WRITE_REG(cd, CD1400_COR5, 0);
  1090. /*
  1091. * if automatic RTS handshaking enabled, set DTR threshold
  1092. * (RTS and DTR lines are switched, CD1400 thinks its DTR)
  1093. */
  1094. if (ISSET(t->c_cflag, CRTSCTS))
  1095. mcor1 = MTTY_RX_DTR_THRESHOLD;
  1096. /* set up `carrier detect' interrupts */
  1097. if (cd->cd_parmode) {
  1098. SET(mcor1, CD1400_MCOR1_DSRzd);
  1099. SET(mcor2, CD1400_MCOR2_DSRod);
  1100. } else {
  1101. SET(mcor1, CD1400_MCOR1_CDzd);
  1102. SET(mcor2, CD1400_MCOR2_CDod);
  1103. }
  1104. CD1400_WRITE_REG(cd, CD1400_MCOR1, mcor1);
  1105. CD1400_WRITE_REG(cd, CD1400_MCOR2, mcor2);
  1106. /* receive timeout 2ms */
  1107. CD1400_WRITE_REG(cd, CD1400_RTPR, 2);
  1108. splx(s);
  1109. return (0);
  1110. }
  1111. /************************************************************************
  1112. *
  1113. * MBPP Routines
  1114. *
  1115. * mbpp_match match one mbpp device
  1116. * mbpp_attach attach mbpp devices
  1117. * mbppopen open mbpp device
  1118. * mbppclose close mbpp device
  1119. * mbppread read from mbpp
  1120. * mbppwrite write to mbpp
  1121. * mbppioctl do ioctl on mbpp
  1122. * mbpppoll do poll on mbpp
  1123. * mbpp_rw general rw routine
  1124. * mbpp_timeout rw timeout
  1125. * mbpp_start rw start after delay
  1126. * mbpp_send send data
  1127. * mbpp_recv recv data
  1128. */
  1129. int
  1130. mbpp_match(struct device *parent, void *vcf, void *args)
  1131. {
  1132. struct magma_softc *sc = (struct magma_softc *)parent;
  1133. return (args == mbpp_match && sc->ms_board->mb_npar &&
  1134. sc->ms_mbpp == NULL);
  1135. }
  1136. void
  1137. mbpp_attach(struct device *parent, struct device *dev, void *args)
  1138. {
  1139. struct magma_softc *sc = (struct magma_softc *)parent;
  1140. struct mbpp_softc *ms = (struct mbpp_softc *)dev;
  1141. struct mbpp_port *mp;
  1142. int port;
  1143. sc->ms_mbpp = ms;
  1144. dprintf((" addr 0x%x", ms));
  1145. for (port = 0 ; port < sc->ms_board->mb_npar ; port++) {
  1146. mp = &ms->ms_port[port];
  1147. if (sc->ms_ncd1190)
  1148. mp->mp_cd1190 = &sc->ms_cd1190[port];
  1149. else
  1150. mp->mp_cd1400 = &sc->ms_cd1400[0];
  1151. timeout_set(&mp->mp_timeout_tmo, mbpp_timeout, mp);
  1152. timeout_set(&mp->mp_start_tmo, mbpp_start, mp);
  1153. }
  1154. ms->ms_nports = port;
  1155. printf(": %d port%s\n", port, port == 1 ? "" : "s");
  1156. }
  1157. /*
  1158. * open routine. returns zero if successful, else error code
  1159. */
  1160. int
  1161. mbppopen(dev_t dev, int flags, int mode, struct proc *p)
  1162. {
  1163. int card = MAGMA_CARD(dev);
  1164. int port = MAGMA_PORT(dev);
  1165. struct mbpp_softc *ms;
  1166. struct mbpp_port *mp;
  1167. int s;
  1168. if (card >= mbpp_cd.cd_ndevs || (ms = mbpp_cd.cd_devs[card]) == NULL || port >= ms->ms_nports)
  1169. return (ENXIO);
  1170. mp = &ms->ms_port[port];
  1171. s = spltty();
  1172. if (ISSET(mp->mp_flags, MBPPF_OPEN)) {
  1173. splx(s);
  1174. return (EBUSY);
  1175. }
  1176. SET(mp->mp_flags, MBPPF_OPEN);
  1177. splx(s);
  1178. /* set defaults */
  1179. mp->mp_burst = BPP_BURST;
  1180. mp->mp_timeout = mbpp_mstohz(BPP_TIMEOUT);
  1181. mp->mp_delay = mbpp_mstohz(BPP_DELAY);
  1182. /* init chips */
  1183. if (mp->mp_cd1400) { /* CD1400 */
  1184. struct cd1400 *cd = mp->mp_cd1400;
  1185. /* set up CD1400 channel */
  1186. s = spltty();
  1187. CD1400_WRITE_REG(cd, CD1400_CAR, 0);
  1188. cd1400_write_ccr(cd, CD1400_CCR_CMDRESET);
  1189. CD1400_WRITE_REG(cd, CD1400_LIVR, (1<<3));
  1190. splx(s);
  1191. } else { /* CD1190 */
  1192. mp->mp_flags = 0;
  1193. return (ENXIO);
  1194. }
  1195. return (0);
  1196. }
  1197. /*
  1198. * close routine. returns zero if successful, else error code
  1199. */
  1200. int
  1201. mbppclose(dev_t dev, int flag, int mode, struct proc *p)
  1202. {
  1203. struct mbpp_softc *ms = mbpp_cd.cd_devs[MAGMA_CARD(dev)];
  1204. struct mbpp_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  1205. mp->mp_flags = 0;
  1206. return (0);
  1207. }
  1208. /*
  1209. * Read routine
  1210. */
  1211. int
  1212. mbppread(dev_t dev, struct uio *uio, int flags)
  1213. {
  1214. return (mbpp_rw(dev, uio));
  1215. }
  1216. /*
  1217. * Write routine
  1218. */
  1219. int
  1220. mbppwrite(dev_t dev, struct uio *uio, int flags)
  1221. {
  1222. return (mbpp_rw(dev, uio));
  1223. }
  1224. /*
  1225. * ioctl routine
  1226. */
  1227. int
  1228. mbppioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
  1229. {
  1230. struct mbpp_softc *ms = mbpp_cd.cd_devs[MAGMA_CARD(dev)];
  1231. struct mbpp_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
  1232. struct bpp_param *bp;
  1233. int error = 0;
  1234. int s;
  1235. switch(cmd) {
  1236. case BPPIOCSPARAM:
  1237. bp = (struct bpp_param *)data;
  1238. if (bp->bp_burst < BPP_BURST_MIN || bp->bp_burst > BPP_BURST_MAX ||
  1239. bp->bp_delay < BPP_DELAY_MIN || bp->bp_delay > BPP_DELAY_MIN) {
  1240. error = EINVAL;
  1241. } else {
  1242. mp->mp_burst = bp->bp_burst;
  1243. mp->mp_timeout = mbpp_mstohz(bp->bp_timeout);
  1244. mp->mp_delay = mbpp_mstohz(bp->bp_delay);
  1245. }
  1246. break;
  1247. case BPPIOCGPARAM:
  1248. bp = (struct bpp_param *)data;
  1249. bp->bp_burst = mp->mp_burst;
  1250. bp->bp_timeout = mbpp_hztoms(mp->mp_timeout);
  1251. bp->bp_delay = mbpp_hztoms(mp->mp_delay);
  1252. break;
  1253. case BPPIOCGSTAT:
  1254. /* XXX make this more generic */
  1255. s = spltty();
  1256. CD1400_WRITE_REG(mp->mp_cd1400, CD1400_CAR, 0);
  1257. *(int *)data = CD1400_READ_REG(mp->mp_cd1400, CD1400_PSVR);
  1258. splx(s);
  1259. break;
  1260. default:
  1261. error = ENOTTY;
  1262. }
  1263. return (error);
  1264. }
  1265. /*
  1266. * poll routine
  1267. */
  1268. int
  1269. mbpppoll(dev_t dev, int events, struct proc *p)
  1270. {
  1271. return (seltrue(dev, events, p));
  1272. }
  1273. int
  1274. mbpp_rw(dev_t dev, struct uio *uio)
  1275. {
  1276. int card = MAGMA_CARD(dev);
  1277. int port = MAGMA_PORT(dev);
  1278. struct mbpp_softc *ms = mbpp_cd.cd_devs[card];
  1279. struct mbpp_port *mp = &ms->ms_port[port];
  1280. caddr_t buffer, ptr;
  1281. int buflen, cnt, len;
  1282. int s, error = 0;
  1283. int gotdata = 0;
  1284. if (uio->uio_resid == 0)
  1285. return (0);
  1286. buflen = min(uio->uio_resid, mp->mp_burst);
  1287. buffer = malloc(buflen, M_DEVBUF, M_WAITOK);
  1288. SET(mp->mp_flags, MBPPF_UIO);
  1289. /*
  1290. * start timeout, if needed
  1291. */
  1292. if (mp->mp_timeout > 0) {
  1293. SET(mp->mp_flags, MBPPF_TIMEOUT);
  1294. timeout_add(&mp->mp_timeout_tmo, mp->mp_timeout);
  1295. }
  1296. len = cnt = 0;
  1297. while (uio->uio_resid > 0) {
  1298. len = min(buflen, uio->uio_resid);
  1299. ptr = buffer;
  1300. if (uio->uio_rw == UIO_WRITE) {
  1301. error = uiomovei(ptr, len, uio);
  1302. if (error)
  1303. break;
  1304. }
  1305. again: /* goto bad */
  1306. /* timed out? */
  1307. if (!ISSET(mp->mp_flags, MBPPF_UIO))
  1308. break;
  1309. /*
  1310. * perform the operation
  1311. */
  1312. if (uio->uio_rw == UIO_WRITE) {
  1313. cnt = mbpp_send(mp, ptr, len);
  1314. } else {
  1315. cnt = mbpp_recv(mp, ptr, len);
  1316. }
  1317. if (uio->uio_rw == UIO_READ) {
  1318. if (cnt) {
  1319. error = uiomovei(ptr, cnt, uio);
  1320. if (error)
  1321. break;
  1322. gotdata++;
  1323. }
  1324. else if (gotdata) /* consider us done */
  1325. break;
  1326. }
  1327. /* timed out? */
  1328. if (!ISSET(mp->mp_flags, MBPPF_UIO))
  1329. break;
  1330. /*
  1331. * poll delay?
  1332. */
  1333. if (mp->mp_delay > 0) {
  1334. s = spltty(); /* XXX */
  1335. SET(mp->mp_flags, MBPPF_DELAY);
  1336. timeout_add(&mp->mp_start_tmo, mp->mp_delay);
  1337. error = tsleep(mp, PCATCH | PZERO, "mbppdelay", 0);
  1338. splx(s);
  1339. if (error)
  1340. break;
  1341. }
  1342. /*
  1343. * don't call uiomove again until we used all the data we grabbed
  1344. */
  1345. if (uio->uio_rw == UIO_WRITE && cnt != len) {
  1346. ptr += cnt;
  1347. len -= cnt;
  1348. cnt = 0;
  1349. goto again;
  1350. }
  1351. }
  1352. /*
  1353. * clear timeouts
  1354. */
  1355. s = spltty(); /* XXX */
  1356. if (ISSET(mp->mp_flags, MBPPF_TIMEOUT)) {
  1357. timeout_del(&mp->mp_timeout_tmo);
  1358. CLR(mp->mp_flags, MBPPF_TIMEOUT);
  1359. }
  1360. if (ISSET(mp->mp_flags, MBPPF_DELAY)) {
  1361. timeout_del(&mp->mp_start_tmo);
  1362. CLR(mp->mp_flags, MBPPF_DELAY);
  1363. }
  1364. splx(s);
  1365. /*
  1366. * adjust for those chars that we uiomoved but never actually wrote
  1367. */
  1368. if (uio->uio_rw == UIO_WRITE && cnt != len) {
  1369. uio->uio_resid += (len - cnt);
  1370. }
  1371. free(buffer, M_DEVBUF, 0);
  1372. return (error);
  1373. }
  1374. void
  1375. mbpp_timeout(void *arg)
  1376. {
  1377. struct mbpp_port *mp = arg;
  1378. CLR(mp->mp_flags, MBPPF_UIO | MBPPF_TIMEOUT);
  1379. wakeup(mp);
  1380. }
  1381. void
  1382. mbpp_start(void *arg)
  1383. {
  1384. struct mbpp_port *mp = arg;
  1385. CLR(mp->mp_flags, MBPPF_DELAY);
  1386. wakeup(mp);
  1387. }
  1388. int
  1389. mbpp_send(struct mbpp_port *mp, caddr_t ptr, int len)
  1390. {
  1391. int s;
  1392. struct cd1400 *cd = mp->mp_cd1400;
  1393. /* set up io information */
  1394. mp->mp_ptr = ptr;
  1395. mp->mp_cnt = len;
  1396. /* start transmitting */
  1397. s = spltty();
  1398. if (cd) {
  1399. CD1400_WRITE_REG(cd, CD1400_CAR, 0);
  1400. /* output strobe width ~1microsecond */
  1401. CD1400_WRITE_REG(cd, CD1400_TBPR, 10);
  1402. /* enable channel */
  1403. cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN);
  1404. CD1400_WRITE_REG(cd, CD1400_SRER, CD1400_SRER_TXRDY);
  1405. }
  1406. /* ZZzzz... */
  1407. tsleep(mp, PCATCH | PZERO, "mbpp_send", 0);
  1408. /* stop transmitting */
  1409. if (cd) {
  1410. CD1400_WRITE_REG(cd, CD1400_CAR, 0);
  1411. /* disable transmitter */
  1412. CD1400_WRITE_REG(cd, CD1400_SRER, 0);
  1413. cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTDIS);
  1414. /* flush fifo */
  1415. cd1400_write_ccr(cd, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
  1416. }
  1417. splx(s);
  1418. /* return number of chars sent */
  1419. return (len - mp->mp_cnt);
  1420. }
  1421. int
  1422. mbpp_recv(struct mbpp_port *mp, caddr_t ptr, int len)
  1423. {
  1424. int s;
  1425. struct cd1400 *cd = mp->mp_cd1400;
  1426. /* set up io information */
  1427. mp->mp_ptr = ptr;
  1428. mp->mp_cnt = len;
  1429. /* start receiving */
  1430. s = spltty();
  1431. if (cd) {
  1432. int rcor, rbpr;
  1433. CD1400_WRITE_REG(cd, CD1400_CAR, 0);
  1434. /* input strobe at 100kbaud (10microseconds) */
  1435. cd1400_compute_baud(100000, cd->cd_clock, &rcor, &rbpr);
  1436. CD1400_WRITE_REG(cd, CD1400_RCOR, rcor);
  1437. CD1400_WRITE_REG(cd, CD1400_RBPR, rbpr);
  1438. /* rx threshold */
  1439. CD1400_WRITE_REG(cd, CD1400_COR3, MBPP_RX_FIFO_THRESHOLD);
  1440. cd1400_write_ccr(cd, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
  1441. /* enable channel */
  1442. cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_RCVEN);
  1443. CD1400_WRITE_REG(cd, CD1400_SRER, CD1400_SRER_RXDATA);
  1444. }
  1445. /* ZZzzz... */
  1446. tsleep(mp, PCATCH | PZERO, "mbpp_recv", 0);
  1447. /* stop receiving */
  1448. if (cd) {
  1449. CD1400_WRITE_REG(cd, CD1400_CAR, 0);
  1450. /* disable receiving */
  1451. CD1400_WRITE_REG(cd, CD1400_SRER, 0);
  1452. cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_RCVDIS);
  1453. }
  1454. splx(s);
  1455. /* return number of chars received */
  1456. return (len - mp->mp_cnt);
  1457. }
  1458. int
  1459. mbpp_hztoms(int h)
  1460. {
  1461. int m = h;
  1462. if (m > 0)
  1463. m = m * 1000 / hz;
  1464. return (m);
  1465. }
  1466. int
  1467. mbpp_mstohz(int m)
  1468. {
  1469. int h = m;
  1470. if (h > 0) {
  1471. h = h * hz / 1000;
  1472. if (h == 0)
  1473. h = 1000 / hz;
  1474. }
  1475. return (h);
  1476. }